# Converting signed hex integer to display negative decimal integer on terminal in C code

I am trying to take a signed hex number and display it on the terminal as a negative decimal number so that it can be easily read. I am having a hard time working through the logic on how to make this happen. I am using a PIC16F15345 and Real Term as my terminal. My UART is up and running correctly.

Problem Example:

Hex: B6

Display dec value: -74

I have thought about first converting the hex number to binary by creating a large if statement for each byte and then concatenate them together, but I am not quite sure on where to go next for converting to decimal. I need this code to be efficient and easily readable as it is going into some documentation I have and all of my ideas seem very complex and long. I feel like I am overthinking this.

Thank you for the help

• When you refer to the number you want to 'convert' and display as a "hex integer", do you mean that you have 2 ASCII characters, for example a B and a 6, or do you really just have a number in an 8-bit variable in your code? Jul 8 '19 at 22:50
• I am trying to display the decimal value, and what I have is a received 8-bit value from my radio module that I am storing in a variable in my code. I then want to convert that 8-bit number to its correct negative decimal number for easy readability on the terminal. Jul 8 '19 at 22:55
• Ok - here's the thing - it's not a "hex number". It's just a number in a variable. If the variable type you've put the number into is signed, then the number is signed. What you're really wanting to do is convert or print it as decimal. Typically, printf will suffice - or to be safe and correct, probably snprintf would be better. If you really want to be a sadist (or you think you can/need to optimize things) then you could do the conversion 'manually' with an if for the sign bit and a couple of / division and % modulus operations for the digits. Jul 8 '19 at 23:01
• ... and once you've found the 100's, 10's and 1's using / and %, don't forget to add the ASCII 0 character to make them printable. Jul 8 '19 at 23:14