Hello.
I have some problem to convert HEX String to Integer.
"3132333435363738" I get this string data from serial port and need to show 12345678 on LCD Display.I try to use code as attached.
lcd.clear();
lcd.setCursor(0, 0);
String getAscii="3132333435363738" //get this string data from serial port.
int d=getAscii.length();
for (int i=0;i <= d;i=i+2)
{
String c= getAscii.substring(i,i+2);
int b = c.toInt()+18;
lcd.write(b);
delay(100);
}
This code is work but not if getAscii is not the number such as "3A3B3C3D3E3F" (:;<>=?) or "4A4B4C4D4F" (JKLMO)
How can I convert "4A4B4C4D4F" to show on LCD as JKLMO?
Thank you so much.
plcexperthelp:
Hello.
I have some problem to convert HEX String to Integer.
"3132333435363738" I get this string data from serial port and need to show 12345678 on LCD Display.I try to use code as attached.
lcd.clear();
lcd.setCursor(0, 0);
String getAscii="3132333435363738" //get this string data from serial port.
int d=getAscii.length();
for (int i=0;i <= d;i=i+2)
{
String c= getAscii.substring(i,i+2);
int b = c.toInt()+18;
lcd.write(b);
delay(100);
}
This code is work but not if getAscii is not the number such as "3A3B3C3D3E3F" (:;<>=?) or "4A4B4C4D4F" (JKLMO)
How can I convert "4A4B4C4D4F" to show on LCD as JKLMO?
Thank you so much.
You are doing this in a very clumsy way. You are taking the hex values 30...39, converting them to decimal 30...39 and then adding 18 to make them ASCII 0...9 which, of course, fails for values A-F and any other values that do not "look" purely numeric.
But, if you want to do it like this, try to actually parse the value of each string. Think about the hex value "39" for example.
The "9" is the one's place and the "3" is in the "16's" place. So, 3*16 + 9 gives you 48 + 9 = 57 which is the ASCII code for 9.
Try this:
int a = (c.subString(1,2).toInt() * 16);
int b = (c.subString(2,3).toInt() * 1);
lcd.write ((char) (a + b));
Now, I don't use the Strings library and my subString syntax may be wrong, but the idea is to get the first character of the 2 character string, get it's int value and multiply by 16. Then do the same for the second character and just use it (I showed "multiply by one" just for consistency).
Then, "a + b" is the actual value of the two hex characters.
I agree with Krupski: dump the String library. It has a lot of features that make processing string data easier, but with only a few nano acres of memory, its resource costs are too high. I modified a program I found that shows how you don't need to use the String class:
void setup() {
char str[] = "4A4B4C4D4F";
char c;
int i;
int len = strlen(str);
Serial.begin(9600);
for (i = 0; i < len; i++) {
if (i % 2 != 0) { // More to do?
c = hex_to_ascii(c, str[i]);
Serial.print(c);
} else {
c = str[i];
}
}
}
int hex_to_int(char c){
int first;
int second;
int value;
if (c >= 97) {
c -= 32;
}
first = c / 16 - 3;
second = c % 16;
value = first * 10 + second;
if (value > 9) {
value--;
}
return value;
}
int hex_to_ascii(char c, char d){
int high = hex_to_int(c) * 16;
int low = hex_to_int(d);
return high+low;
}
void loop() {
// put your main code here, to run repeatedly:
}