如何在 C 中将 short 转换为 unsigned char *buf?

How to convert an short to unsigned char *buf in C?

如何在 C 中将 short 转换为 unsigned char *buf

我读了我的数据

short read_raw_data(int addr) {
    short high_byte, low_byte, value;
    high_byte = wiringPiI2CReadReg8(fd, addr);
    low_byte = wiringPiI2CReadReg8(fd, addr + 1);
    value = (high_byte << 8) | low_byte;
    return value;
}

想用

发送
lws_write(struct lws *wsi, unsigned char *buf, size_t len, enum lws_write_protocol protocol);

喜欢

m = lws_write(wsi, myShortConvertedToUnsignedChar + LWS_PRE, 32, LWS_WRITE_TEXT);

怎么做?

只需使用强制转换将指向 value 的指针转换为 unsigned char:

short value = ...;
lws_write(wsi, (unsigned char*)&value, sizeof value, LWS_WRITE_TEXT);