Apologies in advance if this is not the right place for this question please point me in the right direction.
Background:
This is a snippit of a project reading nmea data from a Garmin hiking GPS.
I'm not trying to solve a particular problem here, I'm just trying to understand the current behavour.
I'm trying to understand why the following code returns a different character from what it's set it to.
Any insight here is appreciated.
String nmea = "abcdefg";
int Ascii_Value;
void setup() {
Serial.begin(4800);
}
void loop() {
nmea[3] = "L"; // set nmea[3] to "L"
Serial.println(nmea[3]); // this should output "L", instead it outputs "#"
Ascii_Value = nmea[3]; // find ascii value to see what the new character is
Serial.println(Ascii_Value); // Ascii_value = 35 for "#", this should be 76 for "L"
Serial.println();
Serial.println();
delay(500);
}// end loop
// When declaring an array of type char, you'll need to make it longer
// by one element to hold the required null termination character:
char message[6] = "hello";
For further information feel free to check this out also
/*
Forum: https://forum.arduino.cc/t/issue-with-reading-writing-characters-in-string-array/1198946/2
Wokwi: https://wokwi.com/projects/383743348014536705
*/
char nmea[] = "abcdefg";
int lengthOfNmea = sizeof(nmea)/sizeof(nmea[0]);
void setup() {
Serial.begin(4800);
nmea[3] = 'L'; // set nmea[3] to "L"
Serial.println(nmea);
// Use the function strlen to get the text length
Serial.print("strlen :\t");
Serial.println(strlen(nmea));
// Use the calculation sizeof(nmea)/sizeof(nmea[0]) to get the size in memory
// allocated by char nmea[] = "abcdefg";
// It is one char/byte more than the letters above ...
// because the end of the char string is marked by a zero = '\0'
// that requires an additional entry
Serial.print("sizeof-calc :\t");
Serial.println(lengthOfNmea);
Serial.print("[ ");
for (int i=0; i<lengthOfNmea;i++){
Serial.print(nmea[i],HEX);
Serial.print(" ");
}
Serial.println(" ]");
Serial.print("[ ");
for (int i=0; i<lengthOfNmea;i++){
Serial.print(nmea[i]);
Serial.print(" ");
}
Serial.println("]");
}
void loop() {
}
Results:
abcLefg
strlen : 7
sizeof-calc : 8
[ 61 62 63 4C 65 66 67 0 ]
[ a b c L e f g ]