I have a question about the strtol function: I am reading a hexadecimal string and want to convert it to decimal using strtol. But I don't want to convert the entire string, just a certain section of it.
I was able to get it to work, but only after writing the value i want to a single digit char and then use the strtol function. I am not sure why it doesn't work just using the indexed array in the first place.
Code:
char rxData[16] = {'1','2','3','4','5','6','7','8','9','10','A','B','C','D','E','F'};
int decValue;
char single = rxData[10];
void setup(){
Serial.begin(9600);
for (int i =0;i<16;i++){
char single = rxData[i];
int decValue1 = strtol(&single,0,16);
int decValue2 = strtol(&rxData[i],0,16);
Serial.print("Array Value:");
Serial.print(rxData[i]);
Serial.print(" - ");
Serial.print("Conversion1:");
Serial.print(decValue1);
Serial.print(" - ");
Serial.print("Conversion2:");
Serial.println(decValue2);
}
}
void loop() {
}
Output:
Array Value:1 - Conversion1:1 - Conversion2:-1
Array Value:2 - Conversion1:2 - Conversion2:-1
Array Value:3 - Conversion1:3 - Conversion2:-1
Array Value:4 - Conversion1:4 - Conversion2:-1
Array Value:5 - Conversion1:5 - Conversion2:-1
Array Value:6 - Conversion1:6 - Conversion2:-1
Array Value:7 - Conversion1:7 - Conversion2:-1
Array Value:8 - Conversion1:8 - Conversion2:-1
Array Value:9 - Conversion1:9 - Conversion2:-1
Array Value:0 - Conversion1:0 - Conversion2:-12817
Array Value:A - Conversion1:10 - Conversion2:-12817
Array Value:B - Conversion1:11 - Conversion2:-12817
Array Value:C - Conversion1:12 - Conversion2:-12817
Array Value:D - Conversion1:13 - Conversion2:3567
Array Value:E - Conversion1:14 - Conversion2:239
Array Value:F - Conversion1:15 - Conversion2:15
Conversion2 goes to the index point but adds the remainder of the values. Why do I have to use char single in my code to get it to work correctly?
Thanks!