帮助在C#\0结尾的字符串 [英] Help with \0 terminated strings in C#

查看:551
本文介绍了帮助在C#\0结尾的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用,我发送一个字节不安全缓冲区指针低水平原生API获得C-字符串值。

I'm using a low level native API where I send an unsafe byte buffer pointer to get a c-string value.

所以,它给了我。

// using byte[255] c_str
string s = new string(Encoding.ASCII.GetChars(c_str));

// now s == "heresastring\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0(etc)";



所以,很显然我不这样做是正确的,我是如何被去掉多余的?

So obviously I'm not doing it right, how I get rid of the excess?

推荐答案

.NET字符串不是空终止(正如你可能已经从这个猜测)。所以,你可以治疗\0',你会对待任何正常的字符。普通字符串操作将修复你的东西。这里有一些(但不是全部)选项

.NET strings are not null-terminated (as you may have guessed from this). So, you can treat the '\0' as you would treat any normal character. Normal string manipulation will fix things for you. Here are some (but not all) options.

s = s.Trim('\0');

s = s.Replace("\0", "");

var strings =  s.Split(new char[] {'\0'}, StringSplitOptions.RemoveEmptyEntries);

这篇关于帮助在C#\0结尾的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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