我需要一个C++函数,它返回解释为bigendian long的四个连续字节的值。指向第一个字节的指针应该更新为指向最后一个字节。我曾尝试下面的代码:在C++中转换long-endian?
inline int32_t bigendianlong(unsigned char * &p)
{
return (((int32_t)*p++ << 8 | *p++) << 8 | *p++) << 8 | *p++;
}
例如,如果p指向00 00 00 A0我希望的结果是160,但它是0。何以见得?
[ntohl()](http://pubs.opengroup.org/onlinepubs/9699919799/functions/ntohl.html)呢? –
[将字节数组(char数组)转换为整数类型(short,int,long)]的可能重复(http://stackoverflow.com/questions/13678166/converting-byte-array-char-array-to-an -integer-type-short-int-long) –
对'p'进行了多次修改,这些修改没有相对于对方进行排序。这是未定义的行为。 –