如何从C#中的字符串中删除NULL字节

编程入门 行业动态 更新时间:2024-10-27 08:38:30
本文介绍了如何从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#中编写了所有程序 谢谢 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# 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字节

本文发布于:2023-11-11 08:11:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577846.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   字节   NULL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!