size_t getPayloadLength(const unsigned char *inputFrame){
size_t payloadLength = inputFrame[1] & 0x7F;
if (payloadLength == 0x7E) {
uint16_t payloadLength16b = 0;
memcpy(&payloadLength16b, &inputFrame[2], 2);
payloadLength = payloadLength16b;
} else if (payloadLength == 0x7F) {
uint64_t payloadLength64b = 0;
memcpy(&payloadLength64b, &inputFrame[2], 8);
payloadLength = (size_t)payloadLength64b;
}
return payloadLength;
}
但是当有效载荷为 == 126 或 127 时,此方法返回错误结果(总是巨大的数字)有人能发现错误吗?
我知道我已经发送了 250 个字符的消息。这是我恢复转换为二进制的前 5 个字节:
[0] 10000001
[1] 11111110 // & 0x7F = 126 -> so payload length is byte 2 3 interpreted as 16 bit
[2] 00000000 //
[3] 11111010 // 0000000011111010 = 250 but my function returns 64000
[4] 10001001