Posts

Showing posts from May, 2019

Converting the ASCII string value of a hex number to a hex number

HEX to ASCII to DECIMAL stuff So this took me way longer than it should have to figure out. Basically, in Python, receiving serial data can be really squirrely. You can receive a hex or decimal character as serial input, and then Python will print it out as a string of that number, and not the corresponding ASCII character. For example, if I send over \x04, which is equivalant to decimal 4, Python receives that variable, and then sets it to string '04', which is incorrect. I'm assuming this is because the ASCII value of \x04 does not exist. Anyways, to re-convert it back to hex, I found that the following method worked: x = '04' hex_val = int ( '0x{}' .format(x), 16 ) print ( 'hex_val=' , hex_val) hex_val= 4 Trying to format it using the method int('\x{}).format(x), 16) does not work. Hope this helps someone! -LL