如何从C#中的字符串中删除NULL字节 [英] How to remove NULL byte from String in C#

查看:437
本文介绍了如何从C#中的字符串中删除NULL字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我想从要搜索关键字的字符串中删除空字节.

这是节目表:->

1)我使用任务管理器创建了程序进程内存的转储文件.然后将转储文件加载到另一个程序中,并将转储文件的所有内容复制到字符串中.

2)现在我想在此字符串中搜索关键字.例如"system.security.key",但实际上以字符串形式显示为"s y s t e m.s e c u r i t y.k e y"(所有空格实际上都是空字节)

3)因此,如果我从中删除空字节,则该关键字看起来像字符串中的"system.security.key".因此我的程序可以搜索该关键字.
4)我试图用Unicode编码,但是它确实成功了,但这不是我想要的,因为它还会更改字符串中我的"system.security.key"旁边放置的"key",这实际上是我正在搜索的.

我在Visual C#.net中编写了所有程序

谢谢

Harsimran

Hi guys
I want to remove null byte from string in which i am searching for keywords.

here is the secnario:->

1) I created a dump file of my programs process memory using task-manager. and then I load dump file into the my other program and copied all content of dump file into string.

2) Now i want to search for keyword in this string. e.g "system.security.key" but in string its is actually presented like "s y s t e m . s e c u r i t y . k e y" ( all spaces are actually null byte )

3) so if i remove null byte from it so that keyword look like "system.security.key" in the string. and thus my program can search this keyword.
4) I tried to encode in Unicode and it does successfully but that is not what I want as it also change "key" that is placed next to my "system.security.key" in a string, which is actually i am searching for.

I program all this in Visual C# .net

Thank you

Harsimran

推荐答案

在C#和所有其他.NET语言中,文本存储在字符串中,并由字符组成,这些字符是16位的数字,支持各种各样的字母,数字和符号. 7位ASCII集中确实存在的那些字符用16位Unicode中的相同数字值表示,产生一个低字节保存ASCII值,高字节保存为零.

如果您认为不需要Unicode,那么就不要使用字符串来表示数据.请改用字节数组.

:)
In C# and all other .NET languages text is stored in strings, and consists of characters, which are 16-bit quantities supporting a wide assortment of letters, digits and symbols. Those characters that do exist in the 7-bit ASCII set are represented by the same numeric value in 16-bit Unicode, yielding a low-byte holding the ASCII value and a high-byte of zero.

If you think you don''t need Unicode for what you are after, then don''t use strings to represent your data. Use byte arrays instead.

:)


string result = input.Replace("\0", string.Empty);


但是,正如您已经知道的那样,使用Unicode UTF16是一个坏主意.如果确实需要Unicode,则应改用UTF8.


However, as you have already been told, using Unicode UTF16 is a bad idea. If you really need Unicode, you should use UTF8 instead.


这篇关于如何从C#中的字符串中删除NULL字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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