读取文件 十六进制方式显示
读取显示二进制文件
要点:
1: byte 数值范围从0~255
2: "/" 高8位
"%" 低8位
举例:DEC:206 HEX CE
206/16 =12 C(hex)
206%16=14 E(hex)
深入理解二进制、十六进制与十进制的转换
//using System.IO; //using System.Windows.Forms; char[] HexChar = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; using (FileStream fs = new FileStream(@"D:\test.txt", FileMode.Open, FileAccess.Read)) { //将文件以文件流的形式进行保存 using (BinaryReader br = new BinaryReader(fs)) { byte[] IBytes = br.ReadBytes((int)fs.Length); int i=0; foreach(byte receivebyte in IBytes) { char hexH = HexChar[receivebyte / 16];//高8位 char hexL = HexChar[receivebyte % 16];//低8位 //Byte:代表无符号的8位整数,数值范围从0~255 MessageBox.Show("Dec: "+receivebyte.ToString()+" Hex: "+(receivebyte).ToString("X2")); Console.Write(hexH.ToString() + hexL.ToString()); //Console.Write(receivebyte.Tostring("X2")); if ((i + 1) % 16 == 0) { if (i == 0) continue; Console.WriteLine(); }else Console.Write(" "); i++; } } Console.WriteLine(); Console.WriteLine("读取完成!");
作者:儒良设计
-------------------------------------------
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!
![](https://files.cnblogs.com/files/bymeet/V.bmp?t=1713616463&download=true)