Convert HEX string to his ASCII value?

Well, what i want is not hard to understand:

I need a function that convert a string like "48454C4C4F" to another string that contain the ascii value of each number..

exemple:

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

Thats it!

People have been telling you that to use Strings is a BAD IDEA. After a while, your program will crash. There are other, better ways of doing what you propose.

Why is that so difficult to understand?

Do you understand that a char array can be a string? Do you understand that String (note the uppercase 'S') is also a string type, but is terribly inefficient? You have an answer to your question, if you'd just run the code and look at the output. You can convert a String to a char array string if you need to, but rarely is that the case.

Ok, i re-explain from the begining...

I receive data from my GSM module, then i want to send back the received message to who is sending it...

Here is exemple of the code used:

char inchar; 
String ContenuDuSMS = "";     

// Exemple of string received:
// 
// +CMT: "2B31343530333431********","","15/03/24,11:45:20-16" 426F6E6A6F7572

void loop()
{
 if (Serial1.available())   
 {
   while(Serial1.available() > 0)         
   {
     inchar = Serial1.read();
     ContenuDuSMS += inchar;  
     
        // When new line is detected...
        if (inchar == '\n')
        {                   
              if (ContenuDuSMS.substring(0,5) == "+CMT:"){                  //If line start with "+CMT:" this is because a SMS is received  
              EnvoiSMS( ContenuDuSMS.substring(8,31) , "You have sent: "+ContenuDuSMS.substring(60,ContenuDuSMS.length()) ); // Extract the phone number + SMS message and send it to "EnvoiSMS" routine 
                                                                                                                              // envoiSMS is a routine to send a SMS              
              }           
              ContenuDuSMS = ""; // reset my string
        }
    }  
  }   
}


void EnvoiSMS(String message,String telephone)
{  
  Serial1.print("AT+CMGF=1\r");                 // AT command to send an SMS
  delay(2000);
  Serial1.println("AT+CMGS=\""+telephone+"\"");// AT command to give the destination phone number
  delay(2000);
  Serial1.println(message);                // The message that i want to send
  delay(2000);
  Serial1.println((char)26);                    // Finalize with  ^Z, (ASCII code 26)
  delay(2000);    
}

When i use " EnvoiSMS("+12223334444","Bonjour") " It send a text message to phone number"12223334444" and the SMS contain "Bonjour" (I have test it and its working!)

But: " EnvoiSMS("2B3132323233333334343434","426F6E6A6F7572") " is NOT working, i got an error message from my GSM module...

So i need to convert the phone number received: (2B3132323233333334343434) and the message received (426F6E6A6F7572) to ascii before sending it to my GSM module...

So, if you dont want to told me how to convert it, maybe you can help me to modify my code to make it work the way i want? (i want to send back the message received to who is sending it)

Thanks for your help...

Since you are starting from the beginning, write a new program that uses character arrays and standard character array processing functions like strlen(), strtok(), strcat(), etc. instead of Strings.

In the long run, you will have much less trouble with this approach.

jremington: Since you are starting from the beginning, write a new program that uses character arrays and standard character array processing functions like strlen(), strtok(), strcat(), etc. instead of Strings.

In the long run, you will have much less trouble with this approach.

ok, so can you give me an exemple of how i can re-write my code with your approach?

It is still not entirely clear what you actually want to do.

If you just want to read a character string from the module via the serial port and echo that string, here is one way to read the string (see reply #1) http://forum.arduino.cc/index.php?topic=45629.0

You will probably want to look for a line terminator, rather than the maximum length.

jremington: It is still not entirely clear what you actually want to do.

If you just want to read a character string from the module via the serial port and echo that string, here is one way to read the string (see reply #1) http://forum.arduino.cc/index.php?topic=45629.0

You will probably want to look for a line terminator, rather than the maximum length.

What i want is to read the SMS received (in that format "426F6E6A6F7572" ) and send it back in this format "Bonjour" Because my GSM module handle it that way.. (like in the code posted just before)

[u]The GSM module show some received data in HEX format, but when i send data to this module i have to send it in the ASCII format...[/u]

Here is a string received from my GMS module: the last part is the message in hex: +CMT: "2B31343530333431********","","15/03/24,11:45:20-16" 426F6E6A6F7572

but when i want to send data to my module i can not send HEX, the string need to be like this: Serial1.print("AT+CMGF=1\r"); Serial1.println("AT+CMGS=\"+14503334444"\""); Serial1.println("Bonjour"); Serial1.println((char)26);

If my phone send "Bonjour" to my GSM module, the module show : 426F6E6A6F7572

And if the GSM module send "426F6E6A6F7572" as the message, my phone receive "426F6E6A6F7572" and NOT "Bonjour"

So before the GSM module send the data back to the phone i have to convert it... And i dont know how to do that with with your approach...

Thanks again for your help...

If you read the string in, according to the code in reply #1 here then you can translate the hex digits as shown in reply #16 by econjack.

jremington: If you read the string in, according to the code in reply #1 here then you can translate the hex digits as shown in reply #16 by econjack.

Ok i can give a try..

But i dont want to convert the whole reading.. only some data need to be converted, so how can i filter data to convert only the phone number and the received text from this string?

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

By my count, start converting after the 6th quote and any whitespace.

This really isn't hard, just pay attention to the details. Just have the computer do what you would do.

Get the string. Get the first non whitespace character after the 6th quote. (isspace()) Take the Ascii digits two at a time, subtract '0' from each, multiply the first by 10 and add them. append the result to a character string.

KeithRB:
Take the Ascii digits two at a time, subtract ‘0’ from each, multiply the first by 10 and add them.

Shirley you mean 1016, aka 1610?

No, just because the OP calls them "hex strings" does not mean they are. Look at my other post, the data is clearly base 10.

KeithRB: No, just because the OP calls them "hex strings" does not mean they are. Look at my other post, the data is clearly base 10.

Then what value does 6F have (as in 426F6E6A6F7572)? It clearly looks like base 16 to me. 0x6F is ASCII for "o" which is the second letter in "Bonjour".

If the space characters are always in the same place, strtok() might be useful, as follows (try it and see what happens):

char *msg="+CMT: ""2B31343530333431********"","""",""15/03/24,11:45:20-16"" 426F6E6A6F7572";
char srch[]=" ";  //search string is single blank
char *token;

void setup() {
  Serial.begin(115200);
  Serial.println(msg);

  // first token
  token = strtok(msg, srch);

  // separate out other tokens
  while( token != NULL ) 
  {
    Serial.println(token );
    token = strtok(NULL, srch);
  }
}
void loop(){
}

A useful summary of string processing routines is found here.

Forget it, he is convinced that he really need to use Strings, so I give up trying to dissuade him and wrote a function in another topic he made (as he is french like me). I think this is what he is looking for:

String hexToAscii( String hex )
{
  uint16_t len = hex.length();
  String ascii = "";

  for ( uint16_t i = 0; i < len; i += 2 )
    ascii += (char)strtol( hex.substring( i, i+2 ).c_str(), NULL, 16 );
    
  return ascii;
}

(as he is french like me)

A French Canadian I know is stubborn.

I created String based full hex convert solution.

It 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;*
    }