As you all know, an int or unsigned int is made up of 4 bytes, (The Learning Guide for C/s + +, chapter 3rd, section 3.2.3: Memory view of variables)
Like what
int n = sizeof (int); N is 4
You can also clearly see in memory, that 4 bytes is there, ("C-VC2008 Learning Guide," Appendix: Debugging Method)
But the question is: How do you turn the code into 4 bytes?
Method 1:memcpy
Such a method is very violent and unscientific. Let's try it first.
unsigned int a = 0x12345678;
unsigned char buf[4];
memcpy (buf, &a, 4);
Observing the values of the 4 elements of the BUF array, is not the 0x78 0x56 0x34 0x12 (well, the small end, which is the flaw, the result we want is actually buf[0]=0x12 buf[1]=0x34 buf[2]=0x56 buf[4]=0x78)
Method 2: Formal Methods ("C + + Learning Guide", chapter 6th, section 6.8)
Buf[0] = a >> 24;
BUF[1] = a >> 16;
BUF[2] = a >> 8;
BUF[3] = A;
Use your VC to see if it is right for you. Look directly in the debug state, stop printf.
No thanks, it's supposed to be!
C + +: How to turn an int into 4 bytes?