Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#从文件中读取所有字节_C#_Arrays_File_Hex_Byte - Fatal编程技术网

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

C#从文件中读取所有字节,c#,arrays,file,hex,byte,C#,Arrays,File,Hex,Byte,这是我想做的。我有一个十六进制值的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(ne

这是我想做的。我有一个十六进制值的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
,但它总是像字符串一样读取我的文件


如何做到这一点?

如果您的文件包含字符串内容(以十六进制格式表示字节),则可以将其读取为string 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();