从 System.Text.Encoding.Unicode.GetString 获取以 null 结尾的字符串

发布于 2024-07-19 19:26:09 字数 526 浏览 7 评论 0原文

我有一个从外部实体收到的字节数组。 它是固定大小的。 这些字节包含一个 unicode 字符串,用 0 值填充缓冲区的其余部分:

所以这些字节可能是:

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

我正在获取该缓冲区并将其转换为字符串,如下所示:

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

我返回的是一个看起来像这样的字符串这个:

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

我如何告诉 GetString 在第一个 Unicode null 处终止字符串(即我只返回“HELLO”)?

感谢您的任何意见。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

樱&纷飞 2024-07-26 19:26:09

如果您确定其余部分都是 \0,那么这将起作用:

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

否则,如果您只想获取第一个 null 之前的所有内容:

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

请注意 Unicode.GetString 将处理双 \0。 您应该只查找单个 \0。

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.

深白境迁sunset 2024-07-26 19:26:09

对于 UTF8/ASCII 编码,您可以通过查找缓冲区中第一次出现的空终止符(使用 System.Array.IndexOf)来实现此目的,而无需重新处理字符串。 然后,您可以使用重载的 System.Text.Encoding.Unicode.GetString 方法创建一个达到给定缓冲区大小的字符串。

下面的示例也适用于不包含空字节的缓冲区:

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);

对于 UTF16,您可以使用类似的 for 循环方法(查找第一对空字符...例如 if (buffer[i] == (byte)0 & buffer[i] == buffer[i+1])

如果不关心创建临时字符串,那么接受的答案是最好的解决方案。

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.

萝莉病 2024-07-26 19:26:09

最简单的方法是在转换后修剪字符串,正如已经建议的那样。

如果您提前知道字符数,则可以使用 GetString 重载,该重载采用起始索引和字节数,以便获取正确的字符串,而无需修剪。

如果您事先不知道字符数,并且希望避免事后修剪字符串,则需要先修剪字节数组,这样您只传递您感兴趣的字节。对于 Unicode,这意味着删除任何字节在第一对零之后(包括第一对零)。

The easiest way would be to trim the string after conversion, as already suggested.

If you know the character count in advance, you could use the GetString overload that takes a start index and a count of bytes, in order to get the correct string, without trimming.

If you do not know the character count in advance, and would like to avoid trimming the string afterwards, you need to trim the byte array first, so you only pass the bytes you are interested in. For Unicode, this would mean removing any bytes after and including the first pair of zeroes.

这样的小城市 2024-07-26 19:26:09

您可以从 Stream.Read() 获取长度。 在这种情况下,流中的 \0 将不被计算在内,您将得到长度 5。然后您可以使用 Encoding.UTF8.GetString 按长度修剪字符串。

int length = peerStream.Read(buffer, 0, buffer.Length);
receive = Encoding.UTF8.GetString(buffer, 0, length);

You can get the length from Stream.Read(). In this case, the \0 from the stream will not be counted and you will get a length of 5. Then you can trim your string with Encoding.UTF8.GetString by the length.

int length = peerStream.Read(buffer, 0, buffer.Length);
receive = Encoding.UTF8.GetString(buffer, 0, length);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文