Is there a utility to aid debugging by producing ascii hex from char array?

I'm trying to debug some code using the RadioHead library to communicate between multiple Feather M0s.

I'm trying to figure out why the receiver is seeing something different from the receiver. So I want to do something like:

     uint8_t buf[RH_RF69_MAX_MESSAGE_LEN];
      uint8_t len = sizeof(buf);

     if (rf69.available()) {
        if (rf69.recv(buf, &len)) {
          Serial.print("ino got packet: '");Serial.print((char*)buf);Serial.print("' len: ");Serial.println(len);
          Serial.print("                ");Serial.println(**hex2Ascii**(buf));
         } else {
          Serial.println("Receive failed");

The problem is I can it figure out how to write that hex2Ascii function.

I've tried several approaches.

First passing a char ptr, and a max length. and then looping through the array char by char, and trying something like:

char* hex2Ascii(char* hexstring) {
  char output[RH_RF69_MAX_MESSAGE_LEN * 3];
  char* outPtr = output;
  char* input = hexstring;
  while (input) {
       char bite = input++[0];
       snprintf(outPtr, 2, "%x", bite);
       outPtr += 2;
 return output;

But this hangs.

Second, trying to use String and concat to cons up the result, but I can't figure out how to concat non const c strings to String.

I've got many years of C experience, albeit decades old, and very little C++.

Any ideas would be appreciated.

We used to tell programmers with problems to "RTFM", but I will be kind and give you The link to the FM.


I suspect that the primary reason is:


Where you meant:

while (*input)

But even then, do you have any guarantee that the input is null terminated?

Returning a pointer to a local variable isn't helping either.

    // Serial.println(**hex2Ascii**(buf));
    for (uint8_t i = 0; i < len; i++)
      if (buf[i] < 0x10)
        Serial.print('0'); // Leading zero
      Serial.print(buff[i], HEX);
      Serial.print(' ');

Did you mean "%02x"?

Thank that helps

1 Like