C#从文件中读取所有字节

问题描述 投票:0回答:1

这是我想做的。我有十六进制值的txt文件。像这样:08-08-CE-FE-CC-D1-05-00-01-00-47-72-50-6F-69-6E-74-30-31。然后,我想读取这些字节并执行以下操作:

byte[] newArray = { bytes[5], bytes[6], bytes[7], bytes[8], bytes[9], bytes[10], bytes[11], bytes[12] };
double newData = (BitConverter.ToDouble(newArray, 0) * 180);

我尝试过StreamReader,但它总是像字符串一样读取我的文件。

我该怎么做?

c# arrays file hex byte
1个回答
0
投票

如果文件中包含字符串内容(以十六进制格式表示字节),则可以将其读取为字符串ant,然后使用以下命令将其转换为字节:

 var hexaStringFromFile = "08-08-CE-FE-CC-D1-05-00-01-00-47-72-50-6F-69-6E-74-30-31";
 var bytes = hexaStringFromFile.Split('-')
     .Select(s => Convert.ToByte(s, 16))
     .ToList();
© www.soinside.com 2019 - 2024. All rights reserved.