Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting null terminated string from System.Text.Encoding.Unicode.GetString

I have an array of bytes that I receive from an external entity. It is a fixed size. The bytes contain a unicode string, with 0 values to pad out the rest of the buffer:

So the bytes might be:

H \0 E \0 L \0 L \0 \0 \0 \0 \0 \0 ... etc 

I'm getting that buffer and converting it to a string like so:

byte[] buffer = new byte[buffSize];
m_dataStream.Read(buffer, 0, buffSize);
String cmd = System.Text.Encoding.Unicode.GetString(buffer);

What I get back is a string that looks like this:

"HELLO\0\0\0\0\0\0\0\0..."

How can I tell GetString to terminate the string at the first Unicode null (ie so I just get back "HELLO")?

Thanks for any input.

like image 309
DougN Avatar asked May 14 '09 16:05

DougN


2 Answers

If you're sure the rest is all \0, this would work:

cmd = cmd.TrimEnd('\0');

Otherwise, if you just want to get everything before the first null:

int index = cmd.IndexOf('\0');
if (index >= 0)
   cmd = cmd.Remove(index);

Note that Unicode.GetString will take care of double \0s. You should just look for a single \0.

like image 128
mmx Avatar answered Sep 21 '22 09:09

mmx


For UTF8/ASCII encodings you can achieve this without reprocessing the string by looking for the first occurrence of the null terminator in the buffer (using System.Array.IndexOf). You can then use the overloaded System.Text.Encoding.Unicode.GetString method to create a string up to the given buffer size.

The example below also caters for a buffer containing no null bytes:

byte[] buffer = new byte[buffSize];
m_dataStream.Read(buffer, 0, buffSize);
var size = System.Array.IndexOf(buffer, (byte)0);
String cmd = System.Text.Encoding.Unicode.GetString(buffer, 0, size < 0 ? buffSize : size);

For UTF16 you could use a similar approach with a for loop (looking for the first pair of null characters ... such as if (buffer[i] == (byte)0 && buffer[i] == buffer[i+1]).

If creating temporary strings is of no concern then the accepted answer is the best solution.

like image 29
Class Skeleton Avatar answered Sep 19 '22 09:09

Class Skeleton