Individual bytes in an int

Does anyone know how to separate the individual bytes in an int? int is 4 bytes long and I want to take those bytes and put them into a char[4].
int is 4 bytes long may not be true in various systems. To take bytes from an int also depend on the system endian-ness. So it is pretty platform-specific. You may want to consult your system installed C/C++ documentation on size of int and endian-ness.
Gotcha. But after I sort those details out is there a C++ function to access only a single byte in a variable?
You can use C/C++ various bitwise operators to do what you want. Or you may want to use C++ STL BitSet class instead. http://www.cplusplus.com/reference/stl/bitset/
Gotcha. But after I sort those details out is there a C++ function to access only a single byte in a variable?


You can do it with pointers. If you create a char pointer, and give it the value of the address of the int, you can then use pointer arithmetic to move the char pointer along the int as you wish. Since a char is one byte long, you can then use that pointer to read single byte values.
Thanks those are both useful answers.

Since a char is one byte long


In general and in fact most of the times the above is true but I was thinking does the C++ standard guarantee a char is always one byte long ? Is it possible for a system to represent char as 2 bytes or whatever?

Is it wiser for use to use sizeof(char) to determine the size of a char and int and long etc ?
In general and in fact most of the times the above is true but I was thinking does the C++ standard guarantee a char is always one byte long ?


Yes, that is guaranteed by the C++ standard. The char is guaranteed to be the smallest single addressable unit of memory, which is a byte. Depending on your architecture that may or may not be 8 bits, but a char is a byte in C++.

Is it wiser for use to use sizeof(char) to determine the size of a char and int and long etc ?


sizeof(char) is one in C++.
Last edited on
Does anyone know how to separate the individual bytes in an int? int is 4 bytes long and I want to take those bytes and put them into a char[4].


This code shows grabbing the data a byte at a time (and displaying is interpreted as a char - if you want numbers, you can cast it from a char).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
 
int main()
{
 
int a = 0x45464748;
char* pChar;
 
pChar = (char*)&a;
 
std::cout << pChar[0];
std::cout << pChar[1];
std::cout << pChar[2];
std::cout << pChar[3];

return 0;
}  
That approach gives results based upon your processor architecture (and current operating mode, even, for machines capable of switching endianness while running).

1
2
3
4
5
6
7
8
void int_to_char4_le( int x, unsigned char bytes[ 4 ] )
  {
  for (int n = 0; n < 4; n++)
    {
    bytes[ n ] = (x & 0xFF);
    x >>= 8;
    }
  }

To get the big-endian version, just reverse() the array you get from the function.

Hope this helps.

[edit] Related post: http://www.cplusplus.com/forum/beginner/31584/#msg171056
Last edited on
Also, you can use unions:
1
2
3
4
5
union
{
   int _int;
   char _byte[sizeof(int)];
};
...which is the same as (char*)&a
And if you don't want to worry about endianness, use bit shifts and masks instead of pointers.

someInt&0xFF will always return the 8 least significant bits of an int for example, regardless if those 8 bits are at the start or the end of the int's address
(someBit>>8)&0xFF will be the second least significant byte, and so on

Note also that char doesn't exist on all processors. On a DSP i use, the smallest addresable size is 2 bytes, so the char type has not been implemented in the DSP's compiler
And if you don't want to worry about endianness, use bit shifts and masks instead of pointers.

And?!! What am I, not here? Didn't I just post that!?

Note also that char doesn't exist on all processors. On a DSP...

If the OP is programming specialized hardware with limitations on the language, he'll know that already. I doubt someone who is struggling with an elementary problem is not learning to program on something other than a normal PC or Workstation.
Take it easy, Duoas.
Last edited on
Thanks for the input guys. I think I'm going to experiment all the suggestions and see which one fits best. I'm looking for a way to store the data from any kind of data structure in one universal way. Then it can be extracted from the char storage and put back into it's native format when the data is needed.
And done. I decided the pointer method suited my purpose best. The data was never going to used once in the char format, only stored and then later retrieved using the reverse of whatever method I chose. Thanks again for the help.
Ah sorry Duoas, i missed this post i have to admit
Sorry I got upset.
Topic archived. No new replies allowed.