ASCII to char

Hallo guys !
I have a problem with my arduino.
I have ASCII in txt and i need to get it to string, but i have:

String txt = "005000720065010D006F00200074006F0020006E0065006601480075006A006701610069006A0065";

then i have this:

 	int i = 0;
	String Val = "";

	
	while (txt.substring(i,i+4) != NULL)
	{
	    Val = txt.substring(i, i+4);
		int test = Val.toInt();
		Serial.println(char(test));
		i = i + 4;
	}

But the problem is, i can get 0050 to 0x0050 and get it to string, but i dont know how to get 010D to this form ... My goal is get String txt to char. Can you help me ?

Don't use the String class (with a capital S). stick to c-strings and the functions from stdlib.h and string.h

how do you get the initial data in your string is the real question and why do you store it this way? ASCII is likely coded on 2 chars like 0xYY where YY is the ASCII Hex representation of the char, not 3 or 4 or whatever - how come do you have 00 in front of some codes? or what is it exactly you have in your String?

Because thats my output from SIM800 ... i know how to parse it to 0x.... but i dont know how to get char from it.

Try to avoid the String class and use C strings instead, as suggested earlier. This might help as a starting point:

#define SUBSTRINGSPLIT 4   // Number of chars in a substring

void setup() {
  char input[] = "005000720065010D006F00200074006F0020006E0065006601480075006A006701610069006A0065";
  char output[sizeof(input) / SUBSTRINGSPLIT + 1];   // Don't forget NULL at end of string
  char temp[5];
  int length;
  int index = 0;
  int i;

  Serial.begin(9600);
  
  length = strlen(input);
  for (i = 0; i < length; i += SUBSTRINGSPLIT, index++) {
    strncpy(temp, &input[i], SUBSTRINGSPLIT);
    Serial.print(temp);
    output[index] = atoi(temp);
    Serial.print("   ");
    Serial.println((char) output[index]);
  }
}

void loop() {

}

Not all of the input are printable characters.

lukeesvk:
Because thats my output from SIM800

come on... that's your representation of the output of your SIM800. The SIM 800 sends you ASCII char, so just bytes. The way you store that is clearly the problem...

show the code you use to read from the SIM800... I suppose you read the data in an int or long and that's what you store in your string in a weird format?

Its normaly SIM.readString();
Im not home, but what i sended here, that’s UCS2 format from parsed readstring ... srsly ... i can give printscreen in 5hrs

I don't need an image/printscreen. We work from code here - that's what your Arduino runs..

Your SIM800 is connected to a serial port, just listen to the serial port to see what's coming
check Serial Input Basics to see how to handle the Serial line correctly

Ok man, so …
I have configured SIM with AT+CSCS=“UCS2”, because i had to send char like š,č,ž,…
I receive readsting(); in hex like 0050072 - that’s first 2letters from message

thanks econjack, i will try it

lukeesvk:
Ok man, so ...
I have configured SIM with AT+CSCS=“UCS2”, because i had to send char like š,č,ž,...

Ok man :slight_smile:

So missed an important info - you don’t have an ASCII communication at all... you changed the char set to UCS2.. so you need a table decoding UCS2 to ascii (UTF-8 might be easier, never tried UCS2)

but i cant set communication in UTF-8... there is only available: HEX,BIN,UCS2,GSM,8859-1, but letters which i need support only 8859-1 and UCS2

lukeesvk:
but i cant set communication in UTF-8... there is only available: HEX,BIN,UCS2,GSM,8859-1, but letters which i need support only 8859-1 and UCS2

Thing is, your goal is impossible because a char can't contain a UCS2 because it's 2 bytes long. A char is one byte long. All you can do is convert the hex ASCII to a UCS2 string and store it. That is just a step by step conversion operation.

I just need to get from 0x010D č … and no fuction exist for that …
I found this:

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

but when i give there 010D … nothing. It can just give me normal chars like 0050(P) etc.

I receive this from SIM:

AT+CMGR=5
+CMGR: "REC READ","+421912345678","","17/11/30,20:34:06+04"
005000720065010D006F00200074006F0020006E0065006601480075006A006701610069006A0065

OK

And i want to get from 00500072... normal characters.

Of course there is no function. There isn't a standard 8 bit character set that will represent those characters. There could be in theory, but since none exists you would have to implement it yourself.

Yea man ... i don't know that there is no fuction for this ... that's the reason why i'm here ... use logic bro.
Look ... when you don't want help, just don't write, if you want, give me some idea how to make it. Thanks

lukeesvk:
And i want to get from 00500072... normal characters.

Look up the ASCII table. Those are the only characters you can map to. There are 8 bit sets that include 128 extra characters but I doubt that any of them would happen to include all the special ones you are using.

lukeesvk:
Yea man ... i don't know that there is no fuction for this ... that's the reason why i'm here ... use logic bro.
Look ... when you don't want help, just don't write, if you want, give me some idea how to make it. Thanks

I am presenting logic. The only way to do what you ask, is to create a custom 8 bit character set that includes all the characters you want to represent. But now, I have to ask, how would you display them? What do you intend to do with the 8 bit string once you have assembled it? Does it have to be human readable at any future point?

I just want make a normal sentence from that ... I need to get it in normal ascii form and upload it to database.
Ok i'm going to make my own character set :confused:

lukeesvk:
I just want make a normal sentence from that … I need to get it in normal ascii form and upload it to database.
Ok i’m going to make my own character set :confused:

What is the point? Why not just store the UCS2 string, as it is standardized and presumably printable by existing systems?

Because its main company ... i need received sms in classic text for employee.
Maybe, its possible to do this transform in C# ? cause im doint this througt webservice.