字符串中的NULL不能在NUL之后复制内容 [英] NULL in a string can't copy contents after NUL

查看:301
本文介绍了字符串中的NULL不能在NUL之后复制内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从外部设备以字节为单位获取数据然后将其转换为字符串时,我遇到了一个小问题.我正在通过TcpClient与该设备通信.

I have a slight issue when I am getting data back from an external device in bytes and then converting it to a string. I am communicating to this device via a TcpClient.

我从此外部设备收到一条消息,并将其格式化为字符串.我遇到的问题是,我收到的消息中包含"NUL" .当我尝试读取其内容时,字符串在遇到字符串中的NUL后终止.例如:

I receive a message from this external device and format it as a string. The problem I am getting is, the message I get back contains a "NUL". When I try to read its contents the string terminates after it comes across NUL in the string. For example:

hello world NUL blah blah blah

当我添加观察者时,在变量中看不到blah blah blah,或者如果我进行常规复制并将其粘贴到整个行的窗口中(如上),它将仅复制到hello world.就像它被切断或终止于字符串一样.永远不要碰到这个.

When I add a watcher I don't see blah blah blah in the variable or if I do a normal copy and paste in windows of the whole line (above) it will only copy up to hello world. Its like it is cut off or terminated off the string. Never come across this before.

也许这是我读取byte []数据的方式吗?

Perhaps it is the way I reading the byte[] data?

编辑

https://www.dropbox.com/s/mzr1rvmxiyh4yt3/stack.log

链接到文件(希望这是允许的),并且读取该文件的代码是:

Link to the file (hopefully this is allowed) and the code to read this is:

        StringBuilder str = new StringBuilder();

        using (StreamReader reader = new StreamReader(@"c:\stack.log"))
        {
            str.Append(reader.ReadToEnd());
        }

看到NUL字符后,StringBuilder的内容一无所知.

See the StringBuilder contents doesn't know anything after the NUL character.

预先感谢,奥南.

希望有足够的细节,如果没有的话,我会根据需要添加更多.

Hope there is enough detail if not I shall add more if required.

推荐答案

搜索\ 0并替换为空字符串或空格.

Search and replace \0 with an empty string or space.

这篇关于字符串中的NULL不能在NUL之后复制内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆