Ascii Help

Hi All,

I’m sure there is a very simple answer to this…

I’m using a substring command to extract each character of a string.
I then want to get the Ascii code for said character.

When i run my code, I’m getting weird results and am not sure why.

can someone please explain what is happening here.
See below

Serial.println(‘A’); ← This returns 65 (This is what i expect)

char* val = “A”;
Serial.println(val); ← This returns 272 (??)

char val2 = ‘A’;
Serial.println(val2); ← This returns 65 (This is the right answer, but i don’t understand why )

If you copied that post into the sketch's setup() function (and removed the non-C-code text after each line), you should not get the results you're claiming. Maybe you experimented around a bit, and your post doesn't match your experiment?

char* val = "A";

This reserves two bytes of memory, puts the ASCII value of the letter A in the first byte, puts a zero in the second byte. Perhaps that memory is at address 262. However, Serial.println(), if given a char* type argument, will correctly look at the memory at that address, and keep printing characters as bytes until it finds a zero byte. You should see the letter A on the output. The only way I see you seeing a number like 262 in the output would be if you did char* val = "A"; Serial.println(u[/u]val);

Your third example is the same as the first. The variable reserves a byte of memory and puts ASCII A into it. The function call sends the value in that memory to the function, and the function prints the numeric value of that byte. If you wanted it to print the byte directly, you need to do Serial.println(val2, [u]BYTE[/u]); instead.

Sorry you are correct, This is what i meant to type originally

Serial.println(int(‘A’)); ← This returns 65 (This is what i expect)

char* val = “A”;
Serial.println(int(val)); ← This returns 272 (??)
char val2 = ‘A’;
Serial.println(int(val2)); ← This returns 65 (This is the right answer, but i don’t understand why )

Thanks Halley

I ended up sorting out my issue by using a different method for extracting each individual character.

as stated earlier, I was using the subString() function to pull each character. I changed my code to use the charAt() in the WString library.

Just for interest sake, What would be the correct method of getting the ascii code for char* var = "A";

Thanks again Halley I'm still very new to C and Arduino. :) but loving it :D

char* my_string = "A quick brown fox.";

Serial.println(my_string[0], DEC); // results in 65
Serial.println(my_string[0], BYTE); // results in A

Serial.println(my_string[8], DEC); // results in 98
Serial.println(my_string[8], BYTE); // results in b

Serial.println(my_string[18], DEC); // results in 0, the string terminator

// unsafe!
// Serial.println(my_string[19], DEC); // results in something unpredictable

For the last one, you’d get the value of whatever is in memory at that location. Not a good practice, especially on a microcontroller where some memory locations trigger certain behaviors even when just being read. Modifying values in string constants or at invalid addresses is also not recommended.