Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

GeeYouEye

macrumors 68000
Original poster
Dec 9, 2001
1,669
10
State of Denial
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.
 

DannySmurf

macrumors 6502a
Jul 7, 2005
628
0
Code:
      int number = 10;
	char b[4];
	b[0] = number;
	b[1] = number >> 8;
	b[2] = number >> 16;
	b[3] = number >> 24;
 

GeeYouEye

macrumors 68000
Original poster
Dec 9, 2001
1,669
10
State of Denial
D'oh! Thanks. I knew it was something simple like that.

One more thing, actually, is the following equivalent (if not bounds checkable)? It seems to be in the test case, but I need to be sure:

Code:
int a = 1234;
char *b = (char *)&test; //obvious I have to be careful with this

And if they're equivalent, which is the better way of doing it? It'll probably be for reading only, but if it is used for writing, it'd be more advantageous to alter the int at the same time, which this pointer trick should. If that's the case, can I get a new int with the value if I need to keep it around for backup, and if so, how? (Like I said, major brain fart today)
 

DannySmurf

macrumors 6502a
Jul 7, 2005
628
0
Ah, I should make a caveat to the code I posted earlier: that converts the integer to four bytes in little-endian format.

Assuming that you're running on an x86/x64 machine, I THINK the two bits of code are equivalent, but converting an int to a char pointer isn't the usual way of doing that (normally you'd just pass a pointer to an integer into whatever stream or data you're writing to and the conversion gets done automatically). But if you run both bits of code side-by-side on a Mac, you'll get different results. The code to convert an integer to bytes on a Mac is similar to what I posted earlier, but I don't know what the exact code would be (all of my data formats are little-endian, even on OSX).

The better way of doing it depends on your requirements. If you're looking for simplicity, use the pointer conversion. If you want cross-platform compatibility, do it the long way.

Not sure what you mean by "get a new int with the value," but assuming you mean to convert the bytes back into an integer (again, assuming little-endian):

Code:
int n = (((b[0] | (b[1] << 8)) | (b[2] << 16)) | (b[3] << 24));
 

GeeYouEye

macrumors 68000
Original poster
Dec 9, 2001
1,669
10
State of Denial
^That's exactly what I'm looking for, thanks. And yeah, for now, I am running this on x86 (I'd be using Objective-C/Cocoa with NSNumbers and NSMutableArrays if I could do what I'm doing on a Mac), 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
 

HiRez

macrumors 603
Jan 6, 2004
6,253
2,579
Western US
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
Yeah, I think you could do it eaither way. Prolly easier to just do it during assignment:
Code:
      int number = 10;
        char b[4];
        b[0] = number >> 8;
        b[1] = number;
        b[2] = number >> 24;
        b[3] = number >> 16;
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.
 

GeeYouEye

macrumors 68000
Original poster
Dec 9, 2001
1,669
10
State of Denial
HiRez said:
Yeah, I think you could do it eaither way. Prolly easier to just do it during assignment:
Code:
      int number = 10;
        char b[4];
        b[0] = number >> 8;
        b[1] = number;
        b[2] = number >> 24;
        b[3] = number >> 16;
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.
I just wish I could use Foundation. It'd make my life a lot easier.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.