简体   繁体   English

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

[英]C# Read all bytes from file

Here is what i want to do.这是我想做的。 I have txt file with HEX value.我有十六进制值的 txt 文件。 Like this: 08-08-CE-FE-CC-D1-05-00-01-00-47-72-50-6F-69-6E-74-30-31 .像这样: 08-08-CE-FE-CC-D1-05-00-01-00-47-72-50-6F-69-6E-74-30-31 Then i want read these bytes and do operations like this:然后我想读取这些字节并执行如下操作:

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);

I have tried StreamReader but it always reads my file like string.我试过StreamReader但它总是像字符串一样读取我的文件。

How i can do that?我怎么能这样做?

If you have file with string content (that represents bytes in hexa format) than you can read it as string ant then convert it to bytes using this:如果您的文件包含字符串内容(表示十六进制格式的字节),则可以将其读取为字符串 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM