Quick C/C++ question

Discussion in 'Mac Programming' started by GeeYouEye, Jul 14, 2005.

1. GeeYouEye macrumors 68000

Joined:
Dec 9, 2001
Location:
State of Denial
#1
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.

2. DannySmurf macrumors 6502a

Joined:
Jul 7, 2005
#2
Code:
```      int number = 10;
char b[4];
b[0] = number;
b[1] = number >> 8;
b[2] = number >> 16;
b[3] = number >> 24;
```

3. GeeYouEye thread starter macrumors 68000

Joined:
Dec 9, 2001
Location:
State of Denial
#3
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)

4. DannySmurf macrumors 6502a

Joined:
Jul 7, 2005
#4
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));
```

5. GeeYouEye thread starter macrumors 68000

Joined:
Dec 9, 2001
Location:
State of Denial
#5
^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

6. HiRez macrumors 603

Joined:
Jan 6, 2004
Location:
Western US
#6
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.

7. GeeYouEye thread starter macrumors 68000

Joined:
Dec 9, 2001
Location:
State of Denial
#7
I just wish I could use Foundation. It'd make my life a lot easier.