How to convert a String o another Hex String

Hi, I need to convert a string to another string but in hexadecimal representation, I am using this code to do it but I only manage to convert the first character of the string, what could be my error?

char msgArray[] = "example message";
char HexString[20];

void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.println(msgArray);
  for (int i = 0; i < sizeof(msgArray) - 1; i++)  {
    sprintf(HexString, "%02x", msgArray[i]);
  }
  Serial.println();
  for (int i = 0; i < sizeof(HexString) - 1; i++)  {
    Serial.print(HexString[i]);
  }
  Serial.println();
}

void loop(){

}

This is what i'm getting

65

When should I get this!

6578616d706c65206d657373616765

You're overwriting HexString each time

sizeof(msgArray) - 1;

strlen would make more sense.

Serial.print(x, HEX);  // print as an ASCII-encoded hexadecimal

I already changed it, still I can only get a hexadecimal character

Probably because you're not indexing along HexString.

It's probably a good thing, because HexString isn't long enough.

I don't need to get it out through the serial port, I need to store the converted string in a variable

I'd like to see the justification for that statement

I did not understand?

You didn't understand why you thought you had to store the hex string in a variable?

Easy peasy

    sprintf(HexString, "%02.2x", msgArray[i]);

it does not work

oops! Sorry,

sprintf(HexString, "%2.2x", msgArray[i]);
sprintf(&HexString[i * 2], "%02x", msgArray[i]);

But, like I said, it's not big enough.

I'd still love to hear that justification.

You continually write different values to the same buffer, and don't print it. Oops that was already mentioned... so print it as you go along. There is no need to accumulate characters in a print string as they will be printed consecutively anyway...
it's only needed if you are saving it for some other purpose, that you would need to construct a string containing all the values.

As @AWOL says, you are overwriting HexString each time you convert a character. Use sprintf() and strcat() together. Also as he says, the HexString is not sized for all the converted characters.

char msgArray[] = "example message";
//char HexString[20];
char HexString[sizeof(msgArray) * 2 + 1]; //2 nibbles/byte + null terminator
char tempChars[3];//2 nibbles/byte + null terminator

void setup() {
  Serial.begin(115200);
  Serial.println();
  Serial.println(msgArray);
  for (int i = 0; i < sizeof(msgArray) - 1; i++)  {
    //sprintf(HexString, "%02x", msgArray[i]);
    sprintf(tempChars, "%02x", msgArray[i]);//hex string with two nibbles
    strncat(HexString, tempChars, 2);//append 2 chars
  }
  Serial.println();
  for (int i = 0; i < sizeof(HexString) - 1; i++)  {
    Serial.print(HexString[i]);
  }
  Serial.println();
}

void loop(){

}

Thanks!!! TheMemberFormerlyKnownAsAWOL, was what I was looking for

to use minimal RAM you could use

char HexString[sizeof(msgArray) * 2 - 1]; //2 nibbles/byte + null terminator