Convert HEX string to his ASCII value?

I have a Hex string like this: 74657374

And I want to convert my string "HEX" to his ASCII value: "test"...

String HEX = "74657374";

String ASCIIValue = ??????

How can i easily do that?

Could someone post some example code for this conversion...

Thanks!!!

This is definitely a hack, but it will give you an idea of how to start:

void setup() {
  char input[] = "84698384";
  char temp[3];
  char c;
  int index;
  int i;
  Serial.begin(9600);
  
  for (i = 0; i < sizeof(input); i += 2) {
    temp[0] = input[i];
    temp[1] = input[i + 1];
    temp[2] = '\0';
    index = atoi(temp);
    c = toascii(index);
    Serial.print(c);
  }
}

void loop() {

}

The string provided is for “TEST”.

I have try your this code and its working, but its not exactly what i need, i have try to make it work for my need with no luck..

What i actually need in a function to convert a string (that contain HEX) and result as string...

[u]Here is what i need:[/u] 1 = my source is a STRING that contain HEX, not a char array. 2 = my source can have different length 3 = i want the result to be inside a string

[u]Exemple:[/u]

//Exemple of the function that i need

String HEXstring = "74657374"; // This is my HEX string, can be any hex of any size
String ASCIIValue = "";    //  Used to put the result

// I want to use it that way:
ASCIIValue = ConvertToASCII(HEXstring);

// Now ASCIIValue contain "TEST" (because my hex is "74657374")

Can you help me to build this "ConvertToASCII" function?

use HEXString.toCharArray() or HEXString.c_str() to get the chars, then program as above, with some changes.

See http://arduino.cc/en/Reference/StringObject

Something like:

for (i = 0; i < HEXString.length(); i += 2) {
    char val = input[i] > 0x39 ? (input[i] - 'A') * 16 : (input[i] - '0') * 16;
    val += input[i+1] > 0x39 ? (input[i+1] - 'A') : (input[i+1] - '0');
    Serial.print(val);
    ASCIIValue += val; 
  }

You say you have a “hex string” and then show us an ascii string.

char converttoascii(char c)
{
if (c < 10)
{
return c+‘0’;
} else {
return (c-10) + ‘A’;
}
}

OP: You are aware of the resource costs of using the String class instead of a char array, right?

econjack: OP: You are aware of the resource costs of using the String class instead of a char array, right?

Well i'm using a gsm module and the output received from the serial port is something like: "74657374", so i want to compare this data with another string..

exemple:

if (receiveddata = "test") .....

So to do that i need to "change" the received data into an ascii string (in this case "74657374" changed to "TEST")

I DONT WANT to compare with hex, exemple: if (receiveddata = "74657374") .....

So thats why i need a function to "convert" my string(that contain hex number) into another string (witch contain the ASCII value of the first string number)

for sure i can write a function like : ... if (receiveddata = "74"){ return "T";} if (receiveddata = "75"){ return "U";} etc... ...

But there is probably a better way to do what i want?

KeithRB:
You say you have a “hex string” and then show us an ascii string.

char converttoascii(char c)
{
if (c < 10)
{
return c+‘0’;
} else {
return (c-10) + ‘A’;
}
}

Thanks for your code, but when i try to use it i got an error because your code want a CHAR input, i need something that work with STRING input…

String TempStr ="84698384";
String TempStr2 ="";
TempStr2 = converttoascii(TempStr);

This code give me : error: cannot convert ‘String’ to ‘char’ for argument ‘1’ to ‘char converttoascii(char)’

ASCIIHexToInt(char *s)
{
    int ret = 0;

    for (int i=0; i<strlen(s); i++)
    {
        if ((s[i] >= '0') && (s[i] <= '9')
            ret = (ret << 4) + s[i] - '0';
        else
            ret = (ret << 4) + toupper(s[i]) - 'A' + 10;
    }
    return ret;
}

Regards,
Ray L.

Show your whole code or at least a full example. I doubt you are receiving a hex string from your gsm module... more likely a serie of bytes that YOU convert to a hex string.

Forget about the String class, use C string instead (char arrays).

guix: Show your whole code or at least a full example. I doubt you are receiving a hex string from your gsm module... more likely a serie of bytes that YOU convert to a hex string.

Forget about the String class, use C string instead (char arrays).

You want a whole code?

void setup() {
Serial.begin(19200);
}

void loop() {
String HEXString ="84698384";
String HEXString2 ="8469838484698384";
String ASCIIValue ="";

ASCIIValue = ConvertToASCII(HEXString);
// ASCIIValue now contain "TEST"
Serial.println(ASCIIValue);

ASCIIValue = ConvertToASCII(HEXString2);
// ASCIIValue now contain "TESTTEST"
Serial.println(ASCIIValue);
}

So can you help me to create the function "ConvertToASCII" ?

You are totally confused.

Ascii 'T' is decimal 84, which is Hex 54.

How can we create a function when your specification is screwed up?

Show us an example of the character strings you actually want to compare.

This string:

String HEXstring = "74657374";

is an ASCII string, containing the ASCII characters for decimal digits. You could interpret it in many ways.

As mentioned, it is very unlikely that you are receiving this sort of string from a gsm module.

KeithRB: You are totally confused.

Ascii 'T' is decimal 84, which is Hex 54.

How can we create a function when your specification is screwed up?

Sorry, you are totaly right, i have used wrong number by mistake, but what i really need is a function to convert string that contain HEX

Here is a REAL exemple of a string that i have received from my GSM module (i have put * to mask my phone number):

+CMT: "2B31343530333431********","","15/03/24,11:45:20-16" 48454C4C4F

The last part "48454C4C4F" is "HELLO" so i want a function to convert this code "48454C4C4F" TO "HELLO

something that work like that:

String HEXString ="48454C4C4F"; String ASCIIValue =""; ASCIIValue = ConvertToASCII(HEXString); // ASCIIValue now contain HELLO

Are you sure the GSM is sending an ASCII '4' and ASCII '8' and not the single character 48? That would be really weird. As said before, show the code that receives this string from your GSM.

KeithRB: Are you sure the GSM is sending an ASCII '4' and ASCII '8' and not the single character 48? That would be really weird. As said before, show the code that receives this string from your GSM.

ok, here is the code that i use:

char inchar; 
String ContenuDuSMS = ""; 


if (Serial1.available())   
 {
   while(Serial1.available() > 0)         
   {
   
     inchar = Serial1.read();
     ContenuDuSMS += inchar;  

     
        if (inchar == '\n')
        {                 
          
           Serial.print(ContenuDuSMS);

Take Ray’s code and modify it a little to get:

void setup() {
  char input[] = "48454C4C4F";    // Should translate to HELLO
  char temp[3];
  char c;
  int index;
  int i;
  int val;
  Serial.begin(9600);

  for (i = 0; i < sizeof(input) - 1; i += 2) {
    temp[0] = input[i];
    temp[1] = input[i + 1];
    val = ASCIIHexToInt(temp[0]) * 16;      // First Hex digit
    val += ASCIIHexToInt(temp[1]);          // Second hex digit
    c = toascii(val);
    Serial.print(c);
  }
}
int ASCIIHexToInt(char c)
{
  int ret = 0;

  if ((c >= '0') && (c <= '9'))
    ret = (ret << 4) + c - '0';
  else
    ret = (ret << 4) + toupper(c) - 'A' + 10;

  return ret;
}

void loop() {

}

econjack:
Take Ray’s code and modify it a little to get:

void setup() {

char input = “48454C4C4F”;    // Should translate to HELLO
  char temp[3];
  char c;
  int index;
  int i;
  int val;
  Serial.begin(9600);

for (i = 0; i < sizeof(input) - 1; i += 2) {
    temp[0] = input[i];
    temp[1] = input[i + 1];
    val = ASCIIHexToInt(temp[0]) * 16;      // First Hex digit
    val += ASCIIHexToInt(temp[1]);          // Second hex digit
    c = toascii(val);
    Serial.print(c);
  }
}
int ASCIIHexToInt(char c)
{
  int ret = 0;

if ((c >= ‘0’) && (c <= ‘9’))
    ret = (ret << 4) + c - ‘0’;
  else
    ret = (ret << 4) + toupper(c) - ‘A’ + 10;

return ret;
}

void loop() {

}

This code is not doing what i want: it take a CHAR as input and the output is a CHAR too… As i already said before: I need something that convert from a STRING to a STRING.

KingRamses: This code is not doing what i want: it take a CHAR as input and the output is a CHAR too.. As i already said before: I need something that convert from a STRING to a STRING.

Why? Do you need the program to crash later?

Delta_G: Why? Do you need the program to crash later?

Because my source is a string! its not a char, so i need to convert from a string!