char[] to string conversion
-
I've got
char[] buffer = new char[N];
with some unicode text. The message is less than N and all other elements are 0 in thebuffer
. But when I create string from it withstring str = new string(buffer);
it appends all N elements including terminating zero characters that string looks like:"message\0\0\0\0\0\0\0\0\0\0\0 ... more \0 chars
. Why it does not terminate it?Чесноков
-
I've got
char[] buffer = new char[N];
with some unicode text. The message is less than N and all other elements are 0 in thebuffer
. But when I create string from it withstring str = new string(buffer);
it appends all N elements including terminating zero characters that string looks like:"message\0\0\0\0\0\0\0\0\0\0\0 ... more \0 chars
. Why it does not terminate it?Чесноков
-
I've got
char[] buffer = new char[N];
with some unicode text. The message is less than N and all other elements are 0 in thebuffer
. But when I create string from it withstring str = new string(buffer);
it appends all N elements including terminating zero characters that string looks like:"message\0\0\0\0\0\0\0\0\0\0\0 ... more \0 chars
. Why it does not terminate it?Чесноков
Because C# strings are not null terminated, unlike C / C++. Because the char[] has a length, each character is transferred into a new string of the same length. Use Trim or the buffer convert as Kubajzz suggested.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.