I am trying to convert a char array to integers:
const int LENGTH = 3 * sizeof(int);
char data[LENGTH];
/* data is filled */
for (int i = 0; i < LENGTH; i += sizeof(int)) {
std::cout << "Integer: " << (int)data[i] << std::endl;
}
for (int i = 0; i < LENGTH; i += sizeof(short)) {
std::cout << (short)data[i] << " ";
}
the output is:
Integer: 0
Integer: 0
Integer: 0
0 3 0 3 0 3
I'd expect that if the shorts are not zero so must the integers. Probably the conversion as seen here works for just that one character/byte and not as expected for the folloing 4 bytes. How can I fix that?
To be clear: I want bytes 0 to 3 casted into one integer, then the next (4 to 7) into the next integer and so on...