c++ - Converting 2 chars to its ascii binary code -
i'm reading binary data in character format accelerometer , consists of higher byte , lower byte. it's long time since worked c++ , used higher level stuff. have following function:
short char2short(char* hchar, char* lchar) { char temp[2]; temp[0] = *hchar; temp[1] = *lchar;
how can values converted integer? atoi works different far know (e.g. "21" = 21). can typecast char int? how work higher bit , lower bit? in advance help!
you should store bytes unsigned avoid issues shifting sign bits.
short char2short(unsigned char hchar, unsigned char lchar) { return static_cast<short>(lchar | (hchar << 8)); }
you may want use unsigned short. depends expect.
Comments
Post a Comment