我正在尝试使用 Link2004/Arduino WebSockets 库从作为服务器的 Wemos D1(ESP8266) 发送整数到使用 OkHttpClient 库的 Android 客户端,使用 sendBIN(num, buf, buf_len)
如下:
int n=1234;
uint8_t * buf = (uint8_t *) &n;
size_t buf_len = sizeof(n);
webSocket.sendBIN(0,buf,buf_len); // num is set to 0, ( client # 0 )
但是我在 Android 端得到的不是 1234,而是 d2040000
。
Android 监听器看起来像这样:
public void onMessage(WebSocket webSocket, ByteString bytes) {
output("Received bytes : " + bytes.hex());
}
正如我所说,TextView 输出的结果是 d2040000
而不是发送的 1234。
谁能澄清一下这个困惑?
最佳答案
字节0xd2
、0x04
、0x00
和0x00
是32位整数1234的字节在 little-endian format .
所以 d2040000
是这些字节的十六进制字符串。
关于android - 从 Arduino 到 Android 的 Websocket : send integer instead of text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51968416/