Home > Software design >  How to convert an short to unsigned char *buf in C?
How to convert an short to unsigned char *buf in C?

Time:11-03

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

I read my data

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;
}

and want to send it with

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

like

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

How to do this ?

CodePudding user response:

Just use a cast to convert a pointer to value to unsigned char:

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