HEX和二进制操作

goz*_*wei 4 c++

我有问题要解决,不知道该怎么做.我的程序从带有十六进制值的串行端口字符串(如DFF7DF)接收.我需要将其转换为二进制形式,丢弃前四位,将第五位作为符号位,接下来将12位作为值.

我需要获得正常INT值.

我能够在MATLAB中创建这样的程序,但我需要C++才能在我的linux arm板上运行它.

在此先感谢您的帮助!马尔钦

Bla*_*iev 5

你可以这样做:

unsigned long value = strtoul("DFF7DF", NULL, 16);
value >>= 4; // discard first four bits
printf("Minus sign: %s\n", value & 1 ? "yes" : "no");
printf("Value: %lu\n", (value & 0x1FFF) >> 1);

long newvalue = (value & 1 ? -1 : 1) * ((value & 0x1FFF) >> 1);
Run Code Online (Sandbox Code Playgroud)