String of Ascii character to a string of hex equivalent

how to convert a string of ascii characters to a string of their hex equivalent in Arduino.

You want to achieve what -- ?

Given below a string of ASCII characters:

char myArray[] = "pqr"; // ={0x70, 0x71, 0x72}={'p', 'q', 'r'}; null-byte is automatically inserted at end

When you say that you want string of their hex equivalent -- what does it mean? By definition, a string is an array of ASCII characters terminated by null-byte, and you have it already as your input data. Do you want to make a print out of the values: 70, 71, and 72?

My interpretation is that OP wants the hex representation. If so, loop through the string and use itoa (with radix 16)

// text to convert
char text[] = "hello world";
// buffer to store hext representation
// twice the size of the text to convert plus space for terminating nul character
char buffer[sizeof(text) * 2 + 1];

void setup()
{
  Serial.begin(57600);

  // 'clear' buffer
  buffer[0] = '\0';

  // loop through character array
  for (uint8_t cnt = 0; cnt < strlen(text); cnt++)
  {
    // conver each character to its hex representation
    itoa(text[cnt], &buffer[cnt * 2], 16);

  }
  // print result
  Serial.println(buffer);
}

void loop()
{
}

Please note how buffer is filled. buffer[cnt*2] refers to the element; multiplying by two is needed as the hex representation uses two bytes. The & in front of it makes it a pointer to the element; itoa requires a pointer to the location where to store the result and we calculate the pointer to the next location.

If you need spacing between the hex representations, the buffer array must be bigger to cater for that and instead of multiplying by two you will have to multiply by 3 where you store.

The clearing of the buffer is not necessary in this example but might be necessary in your code.

Output of above code

68656c6c6f20776f726c64

sterretje:
My interpretation is that OP wants the hex representation.

char text[] = "hello world";

Output of above code
68656c6c6f20776f726c64

The execution of the following codes produces the hex representation of the text message "hello world":

void setup()
{
  Serial.begin(9600);
  char text[] = "hello world";
  for (int i = 0; i < sizeof(text)-1; i++)
  {
    Serial.print(text[i], HEX);
  }
}

void loop() 
{
  
}

sm-6.png

sm-6.png

Thank you all.