Memcpy from Byte * to unsigned int Reverse byte order

I have a CFBitVector that looks like '100000000000000' I pass an array of bytes to CFBitVectorGetBits , which then contains the values ​​from this CFBitVector . After this call, bytes[2] looks like this:

 bytes[0] == '0x80' bytes[1] == '0x00' 

This is exactly what I would expect. However, when copying the contents of bytes[2] to unsigned int bytesValue, value is 128 , when it should be 32768 . The decimal value 128 is represented by the hexadecimal value 0x0080 . Essentially, it seems that the byte order is reversed when memcpy executed. What's going on here? Is this just a content issue?

thanks

 CFMutableBitVectorRef bitVector = CFBitVectorCreateMutable(kCFAllocatorDefault, 16); CFBitVectorSetCount(bitVector, 16); CFBitVectorSetBitAtIndex(bitVector, 0, 1); CFRange range = CFRangeMake(0, 16); Byte bytes[2] = {0,0}; unsigned int bytesValue = 0; CFBitVectorGetBits(bitVector, range, bytes); memcpy(&bytesValue, bytes, sizeof(bytes)); return bytesValue; 
+4
source share
1 answer

What's going on here? Is this just a content issue?

Yes.

Your computer is a little oriented. The 16-bit value 32768 will be represented in memory as:

 00 80 

On a small terminal machine. You have:

 80 00 

This is the opposite, representing 128, as you see.

+5
source

Source: https://habr.com/ru/post/1489356/


All Articles