从Byte*到unsigned int的memcpy正在反转字节顺序
memcpy from Byte * to unsigned int Is Reversing Byte Order
我有一个CFBitVector
,看起来像'100000000000000'
我将字节数组传递给CFBitVectorGetBits
,然后它包含来自该CFBitVector
的值。在这个调用之后,bytes[2]
看起来像:
bytes[0] == '0x80'
bytes[1] == '0x00'
这正是我所期望的。然而,当将bytes[2]
的内容复制到unsigned int bytesValue,
时,该值为128
,而该值应为32768
。十进制值CCD_ 10由十六进制值0x0080
表示。本质上,在执行memcpy
时,字节顺序似乎是颠倒的。这是怎么回事?这只是endianness的问题吗?
感谢
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;
这是怎么回事?这只是endianness的问题吗?
是的。
你的电脑很小。16位值32768
在存储器中表示为:
00 80
在一台小型endian机器上。您有:
80 00
相反,正如你所看到的,代表128。
相关文章:
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 当比特(而不是字节)的顺序至关重要时的持久性
- C++ 替换字节数组中项的顺序
- C++ int* 和 char* 字节顺序已交换
- 交换未定义数据类型中的字节顺序
- 以随机可重现的顺序生成非重复字节的快速方法
- 为什么在未由语言本身定义的结构字节中的位字段顺序
- winsock上的蓝牙,如何删除字节顺序标记
- 将网络字节顺序(大字节序)转换为小字节序
- 将主机字节顺序转换为网络字节顺序有时会导致奇怪的结果
- 当在网络字节顺序中放置双精度时,为什么它是在 4 字节块中完成的
- 使用mysql本机函数进行字节顺序转换
- C++:如何交换wchar_t的字节顺序
- reinterpret_cast接收端的'serializing'数据、字节顺序和对齐方式
- 到Arduino的串行通信的字节顺序
- 使用不带Unicode字节顺序标记的iconv进行字符转换
- 从Byte*到unsigned int的memcpy正在反转字节顺序
- 忽略C++中的字节顺序标记,从流中读取
- 为什么 file.write() 不按照我给出的顺序存储字节?C++
- iPhone TCP-IP字节顺序映射