讀取檔案 十六進位制方式顯示
阿新 • • 發佈:2022-04-03
讀取顯示二進位制檔案
要點:
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("讀取完成!");