我的机器是64位的。我的代码如下:
unsigned long long periodpackcount=*(mBuffer+offset)<<32|*(mBuffer+offset+1)<<24|* (mBuffer+offset+2)<<16|*(mBuffer+offset+3)<<8|*(mBuffer+offset+4);mBuffer是无符号字符*。我想得到5个字节的数据,并将数据转换为主机字节顺序。如何避免此警告?
发布于 2013-07-10 11:04:38
有时最好是分成几行,以避免出现问题。您有一个5字节的整数要读取。
// Create the number to read into.
uint64_t number = 0; // uint64_t is in <stdint>
char *ptr = (char *)&number;
// Copy from the buffer. Plus 3 for leading 0 bits.
memcpy(ptr + 3, mBuffer + offset, 5);
// Reverse the byte order.
std::reverse(ptr, ptr + 8); // Can bit shift here instead可能不是最好的字节交换(位移位更快)。我的逻辑可能不是为了抵消,但沿着这些路线的一些东西应该是有效的。
你可能想要做的另一件事是在移位之前转换每个字节,因为你把它留给编译器来确定数据类型*(mBuffer + offset)是一个字符(我相信),所以你可能想把它转换成一个更大的类型static_cast<uint64_t>(*(mBuffer + offset)) << 32或其他东西。
https://stackoverflow.com/questions/17561470
复制相似问题