Convert HEX to equivalent ASCII character

Hi.

I want to do a conversion as follows:

Input: 48 65 6c 6c 6f Output: Hello

... where the hex number 48 is equivalent to 'H' and so on. How do I do that? Thanks :)

Assuming you have two characters c1 and c2 containing the text representation of a hex number representation of a character then…

  char output=0;
  if(c1 <= '9' && c1 >= '0') output += c1-'0';
  if(c1 <= 'f' && c1 >= 'a') output += c1-'a'+0xA;
  if(c1 <= 'F' && c1 >= 'A') output += c1-'A'+0xA;
  output *= 0x10;
  if(c2 <= '9' && c2 >= '0') output += c2-'0';
  if(c2 <= 'f' && c2 >= 'a') output += c2-'a'+0xA;
  if(c2 <= 'F' && c2 >= 'A') output += c2-'A'+0xA;

where the hex number 48 is equivalent to ‘H’ and so on. How do I do that? Thanks

48 65 6c 6c 6f

You are saying 48 is a hex number – hex format of 01001000; correct?
The ASCII Codes Table says that this bit pattern (01001000) is the ASCII code of the character H. The ASCII Codes Table also indicates that 65, 6C, 6C, and 6F are the ASCII codes of the charctaers e, l, l, and o respectively.

I assume that the 5-byte data that you have provided are stored in an array. The following instructions will present the corresponding characters on the Serial Monitor. That means that we will see Hello on the Serial Monitor.

void setup() 
{
  Serial.begin(9600);
  byte myArray[] = {0x48, 0x65, 0x6C, 0x6C, 0x6F};
  for (int i =0; i<5; i++)
  {
    Serial.print((char)myArray[i]); //printing characters : prints: Hello
  }
  Serial.println();  
}

void loop()             
{

}

Hi, thanks for the replies. Sorry I forgot to mention that my input is indeed in an array. Anyways, the second code was exactly what I needed. Thanks for the help, both of you :slight_smile: Have a great day.

Bear in mind that all data in the computer ultimately ends up in binary. Whether you choose to represent that in decimal, hexidecimal, binary, octal, or whatever doesn’t really matter. Change Golam’s code to define the array as a char data type and it works the same:

void setup()
{
  Serial.begin(9600);
  char myArray[] = {0x48, 0x65, 0x6C, 0x6C, 0x6F};
  for (int i =0; i<5; i++)
  {
    Serial.print(myArray[i]); //printing characters : prints: Hello
  }
  Serial.println(); 
}

void loop()             
{

}

The only different is you don’t need the cast in the print statement because I’ve defined the array as a char array instead of a byte array.

Thank you! :)

I created String based full hex convert solution.

I can be used very simple way.

String stringToHex(String input){
String hex = “”;
for(int i = 0 ; i < input.length(); i++){
char c = (char)input*;*

  • hex+= String(c, HEX);*

  • }*

  • return hex;*
    }
    String hexToString(String hex)
    {

  • String text = “”;*

  • for(int k=0;k< hex.length();k++)*

  • {*

  • if(k%2!=0)*

  • {*

  • char temp[3];*

  • sprintf(temp,"%c%c",hex[k-1],hex[k]);*

  • int number = (int)strtol(temp, NULL, 16);*

  • text+=char(number);*

  • }*

  • } *

  • return text;*
    }

  1. Don't use big-S Strings

  2. Have you heard of sprintf() ?