Turning HEX into ASCII


I have just got a pair of wireless transmitters and receivers going, however, the receiver picks everything up in HEX. Any suggestions on turning it into ASCII?


One HEX character can be turned into two ASCII characters.

first = (hex >> 4);
if (first > 0x9) first += 56; else first += 48;
second = (hex & 0xf);
if (second > 0x9) second += 56; else second += 48;

Thanks for the reply, I don’t quite understand how I’d use that code. Heres a better idea of what I’m trying to do;

 uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;

for (i = 0; i < buflen; i++)
        Serial.print(buf[i], HEX);

That prints what is received as HEX, now how would I turn it into ASCII?

Thanks again :slight_smile:

Joseph_hewitt: That prints what is received as HEX, now how would I turn it into ASCII?

Thanks again :)

What do you mean by 'turn into'?

If you just want to print it as ASCII, then lose the HEX argument and cast buf[i] to a char. What kinds of values are you getting?

When I use this:

Serial.print(buf[i], HEX);

I get the data (a temperature in this case) in HEX and when I do this:


I get the same data, but in decimal. I need to be able to see the number (and hopefully get it into a float) so I can graph it, etc.

I think I may have just figured it out. If I put the decimal into an int, and subtract 48 I will be left with the correct number, however, because the temperature has a decimal place (EX: 21.56) I will need to find a way of getting that into a float afterwards.

Edit: sorry I've misread your last post...

Try using Serial.write() instead of Serial.print()

Joseph_hewitt: I need to be able to see the number

Serial.print((char) buf[i]);

This will print the ASCII for the value stored in buf[i]. So for example, if it contains decimal value 49, it will print '1' to the screen.

Using Serial.print((char) buf*); works great and I can see the data properly. How would I then put it into a float rather than just a Serial print?*

What, you receive things like:


And you want to transform this into a float variable, correct? If yes then once your buf variable contain all the characters needed, use atof():

float temperature = atof( buf ); //temperature now equal to 21.570000

Thanks for the reply. I tried that and it didn't work, I got this error: error: invalid conversion from 'uint8_t' to 'const char*'

(I have used atof in another part of the code so I'm not too sure why it's not working this time)

Oh right sorry I should have said that you need to declare buf as:


.. or use:

atof( (char*)buf );

Ok, I've done that and now I get the same error with this code: if (vw_get_message(buf, &buflen)) { (error: invalid conversion from 'char*' to 'uint8_t*')

Grr.. use

atof( (char*)buf );

And use your brain a little more.. :)

Sorry, I'm probably just being stupid now, but where does that code go?

uint8_t buf[VW_MAX_MESSAGE_LEN];


float temperature = atof( (char*)buf );

Thanks! No errors anymore 8)

Unfortunately, I've now done something to the transmitter, so I can't test it yet. Typical. I'm useless at this kind of thing

Thanks for all your help, you've been great! :)