Click here to Skip to main content
15,899,474 members
Home / Discussions / C#
   

C#

 
AnswerRe: Crystal report display problem Pin
Paul Conrad26-Sep-07 17:51
professionalPaul Conrad26-Sep-07 17:51 
GeneralRe: Crystal report display problem Pin
Malcolm Smart26-Sep-07 22:04
Malcolm Smart26-Sep-07 22:04 
GeneralRe: Crystal report display problem Pin
Paul Conrad27-Sep-07 2:35
professionalPaul Conrad27-Sep-07 2:35 
QuestionProgress Bar Pin
Xmen Real 26-Sep-07 14:03
professional Xmen Real 26-Sep-07 14:03 
AnswerRe: Progress Bar Pin
dengboo26-Sep-07 14:11
dengboo26-Sep-07 14:11 
GeneralRe: Progress Bar Pin
Xmen Real 26-Sep-07 14:21
professional Xmen Real 26-Sep-07 14:21 
AnswerRe: Progress Bar Pin
Liam O'Hagan26-Sep-07 19:46
Liam O'Hagan26-Sep-07 19:46 
QuestionHow to write a generic method to convert int, long, double, etc to a byte array? Pin
rookie nick26-Sep-07 12:03
rookie nick26-Sep-07 12:03 
I new C# programmer and I'm attempting to write a FAST generic method that would takes types like short, int, long, float, double etc.... and put them in a byte array at a specified index. It would allow you to specify the endianess you want. I have seen a couple of answers out on the forums, but they all appeared to be handing the of data (or byte arrays)several times. I could define the same method with the specific type I want use and it will compile but then I need to duplicate this for every type that I want to use. I was hoping I could get some help on this so the method is both fast and easy to maintain.



The following is what I would like to do but it doesn't compile because "Cannot take the address of, get the size of, or declare a pointer to a managed type ('T')":
// This method takes data type T and stores it into a byte array starting at the specified index.
// The caller specifies whether the Most Significant Byte (MSB) should be stored at byteArray[index].
// If MSBFirst is false, then the MSB will be stored at byteArray[index + sizeof(T) - 1]
// This method assumes that we are running on a PC which is little endian.
public unsafe static void IntoBytes2 <T> (byte[] byteArray, int index, T data, bool MSBFirst) where T : struct
{
    byte* dataPtr = (byte*)&data;

    // make sure the array is big enough for the type being stored.
    if ((index + sizeof(T) - 1) <= byteArray.Length)
    {
        if (MSBFirst)
            // Reverse the Endianess
            for (int i = index + sizeof(T) - 1; i >= index; i--, dataPtr++)
            {
                byteArray[i] = *dataPtr;
            }
        else
            // Same endianess of platform (little on PC)
            for (int i = index; i < index + sizeof(T); i++, dataPtr++)
            {
                byteArray[i] = *dataPtr;
            }
    }
}



Thanks for any help

- Nick
AnswerRe: How to write a generic method to convert int, long, double, etc to a byte array? Pin
Dave Kreskowiak26-Sep-07 13:10
mveDave Kreskowiak26-Sep-07 13:10 
GeneralRe: How to write a generic method to convert int, long, double, etc to a byte array? Pin
rookie nick26-Sep-07 14:42
rookie nick26-Sep-07 14:42 
GeneralRe: How to write a generic method to convert int, long, double, etc to a byte array? Pin
Judah Gabriel Himango26-Sep-07 17:45
sponsorJudah Gabriel Himango26-Sep-07 17:45 
GeneralRe: How to write a generic method to convert int, long, double, etc to a byte array? Pin
Dave Kreskowiak27-Sep-07 4:25
mveDave Kreskowiak27-Sep-07 4:25 
GeneralRe: How to write a generic method to convert int, long, double, etc to a byte array? Pin
rookie nick27-Sep-07 5:43
rookie nick27-Sep-07 5:43 
GeneralRe: How to write a generic method to convert int, long, double, etc to a byte array? Pin
rookie nick27-Sep-07 6:26
rookie nick27-Sep-07 6:26 
QuestionCreating Textbox with Checkbox Pin
Can_8526-Sep-07 11:41
Can_8526-Sep-07 11:41 
AnswerRe: Creating Textbox with Checkbox Pin
pmarfleet26-Sep-07 11:57
pmarfleet26-Sep-07 11:57 
GeneralRe: Creating Textbox with Checkbox Pin
Can_8526-Sep-07 12:00
Can_8526-Sep-07 12:00 
AnswerRe: Creating Textbox with Checkbox Pin
Xmen Real 26-Sep-07 14:09
professional Xmen Real 26-Sep-07 14:09 
QuestionHow can i know if the string in some RichText Control is too long ? Pin
Yanshof26-Sep-07 11:35
Yanshof26-Sep-07 11:35 
AnswerRe: How can i know if the string in some RichText Control is too long ? Pin
Xmen Real 26-Sep-07 14:19
professional Xmen Real 26-Sep-07 14:19 
GeneralRe: What ?!? Pin
Yanshof26-Sep-07 14:27
Yanshof26-Sep-07 14:27 
GeneralRe: What ?!? Pin
Luc Pattyn26-Sep-07 15:28
sitebuilderLuc Pattyn26-Sep-07 15:28 
QuestionJavaScript : How to use borderstyle Pin
ss.mmm26-Sep-07 11:15
ss.mmm26-Sep-07 11:15 
GeneralRe: JavaScript : How to use borderstyle Pin
Guffa26-Sep-07 11:59
Guffa26-Sep-07 11:59 
AnswerRe: JavaScript : How to use borderstyle Pin
Scott Dorman26-Sep-07 13:18
professionalScott Dorman26-Sep-07 13:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.