hey,
I am working on this project where I need to store a value in an array but it needs to be stored over 4 indexes of the array. The array that I am using to store the number in is a char, so eight bits can be stored at each index.
For example if I have 528968, I need to store eight bits of the number in each index.I have tried the left and right shift operators but am unable to get all bits that the number includes.
1 2 3 4 5 6 7 8 9 10
int num = 528968;
char holder[6];
holder[1] = 0x24(01001000)
holder[2] = 0x12(00010010)
holder[3] = 0x08(00001000)
holder[4] = 0x00(00000000)
If anyone can give me any advice on how to handle this, I will greatly appreciate it.
#include <iostream>
usingnamespace std;
int main()
{
unsignedchar holder[4]; // Make this unsigned!!!
// to split
int x = -528968;
for (int i = 0; i < sizeof(x); i++)
holder[i] = x >> (i*8);
// to reassemble
int y = 0;
for (int i = 0; i < sizeof(y); i++)
y += holder[i] << (i*8);
// Check
cout << "Your X in hex: " << hex << x << endl;
for (int j = 0; j <4; j++ )
cout << "holder[" << j << "]: " << hex
<< static_cast<int>(holder[j]) << endl;
cout << "\nYour Y: " << y << endl << endl;
system("pause");
}