-1
\$\begingroup\$

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

\$\endgroup\$
4
  • \$\begingroup\$ 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? \$\endgroup\$
    – brhans
    Jul 8 '19 at 22:50
  • \$\begingroup\$ 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. \$\endgroup\$
    – jhandy
    Jul 8 '19 at 22:55
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – brhans
    Jul 8 '19 at 23:01
  • \$\begingroup\$ ... 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. \$\endgroup\$
    – brhans
    Jul 8 '19 at 23:14
0
\$\begingroup\$

no conversion is needed, just cast it to signed, then proceed normally.

\$\endgroup\$
0
\$\begingroup\$

There are signed and unsigned variables. 1st) you need to declare signed variable of 8-bit size. If it doesn't exist then you may declare as signed variable of 16bit (or whatever bit value the PIC is made). The MSB bit is a sign bit, so how to make you 8 bit number to get the sign? You do shift left for 8 places and then do arithmetic shift right to 8 places back. With arithmetic shift the sign is preserved. Then use Printf function with the right format.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.