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 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.
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 ?
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, unsignedchar 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.
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.
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.