[Solved] What is the best way of getting the ASCII char from HEX

What is the best way to get convert a HEX value back to ASCII to read a message from the Radio head library?

Also what is the best way to concatenate a number to the end of a char string? (I am testing some stuff and want to add a count number to a message)

If it's four bytes or less I believe strtol (strtoul) will do it. You may have to prefix "0x".

Probably sprintf.

Many thanks, it is a byte at a time... So no problem there.

Sorry for putting the question in the wrong place I thought I had put it in programming. I shall give myself a right telling off and no mistake!

Ah that appears to convert an int to a string, (OK for half my issue) but I need to convert the hex for H to H.

Do you have an example of the kind of format you're on about. Is it a string of hex values or hex values within strings?

I am working with the radio head ASK library examples, specifically the following code to send a message:

void loop()
{
    char *msg = "hello";

    driver.send((uint8_t *)msg, strlen(msg));
    driver.waitPacketSent();
    delay(200);
    Serial.println("Message Sent!");
}

And the following to receive:

void loop()
{
    uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
    uint8_t buflen = sizeof(buf);

    if (driver.recv(buf, &buflen)) // Non-blocking
    {
	int i;

	// Message with a good checksum received, dump it.
	driver.printBuffer("Got:", buf, buflen);
        //Serial.println("Got:", buf);
        for (i = 0; i < buflen; i++)
	{
	    Serial.print(buf[i]);
	    Serial.print(" ");
	}
	Serial.println("");
        digitalWrite(13, false);
    
    }

Which outputs

Got:
68 65 6C 6C 6F 
104 101 108 108 111

What I would like is to convert the individual bits of hex back to something human readable.

Then I would like to add a number to the outbound message as well so I can track and see any losses.

what happens if you change this lineSerial.print(buf[i]);
to Serial.print(buf[i], DEC);

That is still printing the decimal equivalent of the HEX value that is in the buffer.

So here's the thing. They're not "hex" values in the buffer, they're just values. It's just the way they get printed that makes them appear "hex"

You see Hex, Decimal, Binary are just different notations used to express values. The values in your buffer are just values. It's not until you chose to express those values that the notation you choose becomes relevant.

OK... So they went into the pipe as chars how do I view them as such?

char* chars=(char*) buf;
Serial.println(chars);

ChilliTronix:
Ah that appears to convert an int to a string, (OK for half my issue) but I need to convert the hex for H to H.

I assume you are referring to strtoul. Null terminate string to an unsigned long. This...

(char) strtoul( "30", NULL, 16 )

...should return '0'.

KenF:

char* chars=(char*) buf;

Serial.println(chars);

That is interesting. It sort of works but I get extra payload:

The code:

void loop()
{
    digitalWrite(13, true);
    uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
    uint8_t buflen = sizeof(buf);

    if (driver.recv(buf, &buflen)) // Non-blocking
    {
	int i;

	// Message with a good checksum received, dump it.
	driver.printBuffer("Got:", buf, buflen);
        //Serial.println("Got:", buf);
        for (i = 0; i < buflen; i++)
	{
	    Serial.print(buf[i], DEC);
	    Serial.print(" ");
	}
        Serial.println("!");
        char* chars=(char*) buf;
        Serial.println(chars);
        Serial.println("*");
        digitalWrite(13, false);
    
    }

}

Gives an output of:

Got:
68 65 6C 6C 6F
104 101 108 108 111 !
helloæÝvÞ}Ý"¿r³0ºêóar?ЉaaîaîÒaîÒ„Òo Â
*
Got:
68 65 6C 6C 6F
104 101 108 108 111 !
helloæÝvÞ}Ý"¿r³0ºêóar?ЉaaîÒaaîÒ„Òo Â
*

I wonder if it is anything to do with encoding with uint8_t?

The extra payload as you call it is because you do not have a null terminated array. So when recieving simply add a null ( zero ) to what you have recieved before you print it out.

A normal char array is closed with a '\0' at the end, if not you need a manual loop

for (i = 0; i < buflen; i++)
{
   Serial.print( (char)buf[i] );
}
Serial.println();

@robtillaart, that was it! As simple as (char)!