Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在编写一个 tftp 客户端。但是当我按如下方式转换块号时:
uint16_t blockN = 缓冲区[2]<<8 | 缓冲区[3];
在 127 之后,我得到 65408 作为 blockN。这里可能有什么问题?
谢谢您的回答。
您必须将数组的类型buffer从数组更改char为数组 unsigned char,否则buffer[2]将被提升为int并发生符号扩展。在大多数平台上char,类型是签名类型。
buffer
char
unsigned char
buffer[2]
int
我通过 uint16_t blockN = buffer[2]<<8 | 解决了 (缓冲区[3]&0xFF);