i want to transmit data by bit unit so i was access data with char* variable. here is my code.
int main()
{
//initiate int variable and casting with char*
int a = 65;
cout << a << endl;
char* p = reinterpret_cast<char*>(&a);
cout << "------------------" << endl;
//check char* p is pointing &a
cout << &a << endl;
printf("0x%x\n", p);
cout << "------------------" << endl;
//access int variable with byte unit
cout << (int)*(p + 0) << endl;
cout << (int)*(p + 1) << endl;
cout << (int)*(p + 2) << endl;
cout << (int)*(p + 3) << endl;
cout << "------------------" << endl;
//initiate int variable and assemble with char* access in way 1
int* b = new int(0);
*b = *(p + 0) << 24;
*b += *(p + 1) << 16;
*b += *(p + 2) << 8;
*b += *(p + 3);
cout << *b << endl;
cout << "------------------" << endl;
//initiate int variable and assemble with char* access in way 2
*b = *(p + 0);
*b += *(p + 1) << 8;
*b += *(p + 2) << 16;
*b += *(p + 3) << 24;
cout << *b << endl;
return 0;
}
and output like this.
65 -> variable a is 65
------------------
0x61ff04
0x61ff04 -> char* p is pointing right
------------------
65
0
0
0 -> access with byte unit
------------------
1090519040 -> way 1
------------------
65 -> way 2
when i access data by byte unit that first address pointing data shows '65' so i think this system is big endian.
so i thought if i want to transmit 'a' data to variable 'b', then *(p+0) data should be go to first like way 1, but the result isn't right. *(p+0) go at last - way 2, show right value.
in easy way to think, i think i was transmit data in direct memory point to point like this
variable a => variable b
[0x000000] => [0x100000]
[0x000001] => [0x100001]
[0x000002] => [0x100002]
... => ...
i don't know why this happen. is anyone can explain about this?
============================================================================
problem was solved. the system was not big endian. i was mistake it.