Sorry for asking such a simple question, but I'm having a brain fart right now. How do you get the bytes of a 32 bit integer into a 4-byte char array? Thanks.
int number = 10;
char b[4];
b[0] = number;
b[1] = number >> 8;
b[2] = number >> 16;
b[3] = number >> 24;
int a = 1234;
char *b = (char *)&test; //obvious I have to be careful with this
int n = (((b[0] | (b[1] << 8)) | (b[2] << 16)) | (b[3] << 24));
Yeah, I think you could do it eaither way. Prolly easier to just do it during assignment:GeeYouEye said:though if you happen to find out what the code is for big-endian systems (I'd imagine it'd only be a question of performing a byte swap right afterwards, or just changing the assignment order), go ahead and post it for reference
int number = 10;
char b[4];
b[0] = number >> 8;
b[1] = number;
b[2] = number >> 24;
b[3] = number >> 16;
I just wish I could use Foundation. It'd make my life a lot easier.HiRez said:Yeah, I think you could do it eaither way. Prolly easier to just do it during assignment:If you are using Apple's Foundation frameworks, you can also use any of the built-in byteswapping functions, such as NSSwapInt(), NSSwapHostFloatToLittle(), etc. I imagine most base frameworks have similar functionality.Code:int number = 10; char b[4]; b[0] = number >> 8; b[1] = number; b[2] = number >> 24; b[3] = number >> 16;