问题描述
unsigned long Fat_AuthAnswer(char b1,char b2,char b3,char b4)`
{
unsigned char* ptr = NULL;
unsigned short StartCRC = b1 + b2*256;
unsigned long ret = crcbuf(StartCRC,b3,&AuthBlock[b4]);
ret = (ret & 0x0000ffff) | (crcbuf(StartCRC,b4,&AuthBlock[b3])<<16);
}
b1=0xAF b2=0x50
函数执行时StartCRC = b1 + b2*256;产量 StartCRC = 0x4FAF
我原以为 StartCRC 的结果是 0x50AF。
我的问题是为什么 b2 似乎减一? 任何帮助,将不胜感激。谢谢
解决方法
您环境中的 char
似乎是有符号的,并且值 0xAF
被符号扩展到值行 0xFFFFFFAF
。这将导致 0xFF
添加到 b2
的那部分,因此看起来 b2
减一。
您应该将 b1
转换为 unsigned char
以避免这种情况。
unsigned short StartCRC = static_cast<unsigned char>(b1) + b2*256;