Csharp/C#教程:在c#中读取.txt文件的hex值分享


在c#中读取.txt文件的hex值

如果我有一些像abc.txt这样的文本文件,我想要那个.txt文件的hex值,就像我们看到打开notepad +时可以点击hex …这样的东西

74 68 65 72 77 69 73 65 20 69 73 20 69 74 63 68 therwise is itch 69 6e 27 20 66 6f 72 20 61 20 66 69 67 68 74 2e in' for a fight. 

现在我想要单个字母的这些hex值。

我知道如何使用FileStream()StreamReader()来读取文本。

但现在想要这些hex值我怎么能得到这个?

 BinaryReader reader = new BinaryReader(new FileStream("C:\file.ext", FileMode.Open, FileAccess.Read, FileShare.None)); reader.BaseStream.Position = 0x0; // The offset you are reading the data from byte[] data = reader.ReadBytes(0x10); // Read 16 bytes into an array reader.Close(); 

所以假设输入是therwise is itch

 string data_as_str = Encoding.Default.GetString(data); // Output: therwise is itch string data_as_hex = BitConverter.ToString(data); // Output: 74-68-65-72-77-69-73-65-20-69-73-20-69-74-63-68 

使用FileStream打开,然后使用Read获取byte数组。 对于数组中的每个元素,使用byteVal.ToString("x2")转换为hex对(如果需要大写hex,请使用X2 )。

上述就是C#学习教程:在c#中读取.txt文件的hex值分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/991488.html

(0)
上一篇 2021年12月23日
下一篇 2021年12月23日

精彩推荐