C
coltrane
I am trying to convert a buffer to a string using
Encoding.UTF8.GetString(...)
where the buffer contains trailing '\0' s.
When I get the length of the string or print the string the '\0' s are
included as spaces. Why doesn't a string use '\0' to terminate a
string?
example:
byte[] buffer = new buffer[10];
buffer[0] = (byte)'1';
buffer[1] = (byte)'2';
buffer[2] = (byte)'3';
buffer[3] = 0;
string text = Encoding.UTF8.GetString(buffer);
Console.Write('|');
Console.Write(text);
Console.Write('|');
The output is: "|123 |"
actually I am getting the buffer reading from a socket and the buffer
is null terminated and then the rest of the buffer is filled with
junk. When the buffer is converted to a string and printed the text
and the junk are printed.
So I guess the question might be, how do I terminate a string?
thanks for the help
John
Encoding.UTF8.GetString(...)
where the buffer contains trailing '\0' s.
When I get the length of the string or print the string the '\0' s are
included as spaces. Why doesn't a string use '\0' to terminate a
string?
example:
byte[] buffer = new buffer[10];
buffer[0] = (byte)'1';
buffer[1] = (byte)'2';
buffer[2] = (byte)'3';
buffer[3] = 0;
string text = Encoding.UTF8.GetString(buffer);
Console.Write('|');
Console.Write(text);
Console.Write('|');
The output is: "|123 |"
actually I am getting the buffer reading from a socket and the buffer
is null terminated and then the rest of the buffer is filled with
junk. When the buffer is converted to a string and printed the text
and the junk are printed.
So I guess the question might be, how do I terminate a string?
thanks for the help
John