Using String.getBytes(buffer, len) to get char bytes.
"len" is the length to copy, but usually need to add 1 for the end of string '\0'.
For example:
String s = "123";
int count = s.length(); // lenth() is 3
s.getBytes(buffer, count); // copied "12\0" => HEX(31 32 00)
s.getBytes(buffer, count + 1); // copied "123\0" => HEX(31 32 33 00)
The official document not mentioned those above, and itit's not reasonable for the modern function design.
Misunderstanding in that and waste our life so much.