如果我有一些文本文件,如abc.txt,则我要该.txt文件的十六进制值
就像我们打开记事本时看到的一样,可以单击十六进制...

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.


现在我想要这些单个字母的十六进制值。

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

但是现在想要这些十六进制值我怎么能得到呢?

最佳答案

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

关于c# - 在C#中读取.text文件的十六进制值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6729281/

10-10 08:58