I am trying to convert hex data to signed int/decimal and can't figure out what I'm doing wrong.
I need FE to turn into -2.
I'm using Convert.ToInt32(fields[10], 16) but am getting 254 instead of -2.
Any assistance would be greatly appreciated.
intis 32 bits wide, so 0xFE is REALLY being interpreted as 0x000000FE for the purposes ofConvert.ToInt32(string, int), which is equal to 254 in the space ofint.Since you're wanting to work with a signed byte range of values , use
Convert.ToSByte(string, int)instead (byteis unsigned by default, so you need thesbytetype instead).Convert.ToSByte("FE",16)