Safe version of unsafe code
-
I'm getting to grips with C#, but one thing still escapes me If I have a number, 0x12345678, and I need to reorder it for some reason to 0x56781234, I could do this in C/C++
int i = 0x12345678; short *ps = (short*)&i; short s; s = ps [0]; ps [0] = ps [1]; ps [1] = s;
i is then 0x56781234. This sort of problem also pops up when a file has some binary data encoded in such a way that 3 bytes are used to represent a 4 byte int (PowerPoint does this if I remember correctly) All attempts I've tried at this so far have resulted in unsafe code. I'd prefer a safe way, if this is possible. Cheers -
I'm getting to grips with C#, but one thing still escapes me If I have a number, 0x12345678, and I need to reorder it for some reason to 0x56781234, I could do this in C/C++
int i = 0x12345678; short *ps = (short*)&i; short s; s = ps [0]; ps [0] = ps [1]; ps [1] = s;
i is then 0x56781234. This sort of problem also pops up when a file has some binary data encoded in such a way that 3 bytes are used to represent a 4 byte int (PowerPoint does this if I remember correctly) All attempts I've tried at this so far have resulted in unsafe code. I'd prefer a safe way, if this is possible. Cheers -
I'm getting to grips with C#, but one thing still escapes me If I have a number, 0x12345678, and I need to reorder it for some reason to 0x56781234, I could do this in C/C++
int i = 0x12345678; short *ps = (short*)&i; short s; s = ps [0]; ps [0] = ps [1]; ps [1] = s;
i is then 0x56781234. This sort of problem also pops up when a file has some binary data encoded in such a way that 3 bytes are used to represent a 4 byte int (PowerPoint does this if I remember correctly) All attempts I've tried at this so far have resulted in unsafe code. I'd prefer a safe way, if this is possible. Cheers