The correct answer depends on several conventions - is it a hexagonal line big-endian or little-endian? Are you starting to read bits from the most significant or least significant bit? Will there always be exactly 6 hexadecimal characters (24 bits)?
In any case, here is one solution for buy-din, always-24-bit, counting from the most significant bit. I am sure you can adapt it if some of my assumptions are wrong.
int HexToInt(char *hex) { int result = 0; for(;*hex;hex++) { result <<= 4; if ( *hex >= '0' && *hex <= '9' ) result |= *hex-'0'; else result |= *hex-'A'; } return result; } char *data = GetDataFromSerialPortStream(); int rawValue = HexToInt(data); int sign = rawValue & 0x10000; int value = (sign?-1:1) * ((rawValue >> 4) & 0xFFF);
Vilx- source share