寫入整型25 文件在MiniHex中顯示 19 00 00 00 寫入字符串I am happy 0A 49 20 61 6D 20 68 61-70 70 79 49 20 61 6D 20 68 61-70 70 79 這一行數據是C#把字符串轉換為16進制形式 不知道為啥用MiniHex打開多了個0A 寫入空"" 00 在ASCII碼中16進制00代表空字符 寫入空格 " " 01 20 在ASCII碼中16進制20代表空格. 01代表標題開始 string s = " AAA BBB CCC"; 寫入后 0C 20 41 41 41 20 42 42-42 20 43 43 43 0C表示換頁鍵;20表示空格 string s = "AAA BBB CCC"; 寫入后 0B 41 41 41 20 42 42 42 20 43 43 43 0B表示垂直制表符 20表示空格 string s = "A AA BBB CCC"; 寫入后 0C 41 20 41 41 20 42 42 42 20 43 43 43 0C表示換頁鍵;20表示空格 string s = "AA A BBB CCC"; 寫入后 0C 41 20 41 41 20 42 42 42 20 43 43 43 0C表示換頁鍵;20表示空格(還0C開頭) 06 表示收到通知.
private void button1_Click(object sender, EventArgs e) { BinaryWriter bw; BinaryReader br; //字符串到16進制 string s = "I have"; string sHex = ""; byte[] sbytes = Encoding.Default.GetBytes(s); for (int i = 0; i < sbytes.Length; i++) { sHex += sbytes[i].ToString("X2") + " "; } //整型到16進制 int i25 = 25; string i25Hex = ""; i25Hex = i25.ToString("X2"); //浮點數到16進制 double d = 3.14157; string dHex = ""; //dHex = d.ToString("X2");//報錯 byte[] dbytes = Encoding.Default.GetBytes(d.ToString()); for (int i = 0; i < dbytes.Length; i++) { dHex += dbytes[i].ToString("X2") + " "; } bool b = true; string bHex = ""; //create the file bw = new BinaryWriter(new FileStream("mydata", FileMode.Create)); //創建文件 bin目錄下 //writing into the file //bw.Write(i25);//寫入1個25 // bw.Write(d); // bw.Write(b); bw.Write(s);//寫入一個字符串 bw.Close(); MessageBox.Show("ccc"); //寫入'二進制'完成 //reading from the file br = new BinaryReader(new FileStream("mydata", FileMode.Open));//在這里打個斷點, i25 = br.ReadInt32(); d = br.ReadDouble(); b = br.ReadBoolean(); s = br.ReadString(); br.Close(); }
一臉懵逼....... 0A 0B 0C 06 什么鬼
1個字節8位 最大10進制數 127 最小值是-128 0000 0000 1111 1111 = 255 (在0000 0000 1111 1111中是255)在(1111 1111中是-1) 1111 1110 = 254 0111 1111 = 127 1000 0000 = -128 1000 0001 = -127 1000 0010 = -126 2個字節(1個字)16位 max 0111 1111 1111 1111 = 32767 2個字32位 (1個字=2個字節) 0111 1111 1111 1111 1111 1111 1111 1111 = 2147483647 10位數 4個字64位 0111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 = 9223372036854775807 19位數 int -> System.Int32 (整型,占 4 字節,表示 32 位整數,范圍 -2,147,483,648 到 2,147,483,647)
1個字節8位 最大10進制數 127 最小值是-128
0000 0000
1111 1111 = 255 (在0000 0000 1111 1111中是255)在(1111 1111中是-1)
1111 1110 = 254
0111 1111 = 127
1000 0000 = -128
1000 0001 = -127
1000 0010 = -126
2個字節(1個字)16位 max
0111 1111 1111 1111 = 32767
2個字32位 (1個字=2個字節)
0111 1111 1111 1111 1111 1111 1111 1111 = 2147483647 10位數
4個字64位
0111 1111 1111 1111 1111 1111 1111 1111
1111 1111 1111 1111 1111 1111 1111 1111 = 9223372036854775807 19位數
int -> System.Int32 (整型,占 4 字節,表示 32 位整數,范圍 -2,147,483,648 到 2,147,483,647)
C# 創建二進制文件並寫入 BinaryWriter bw = new BinaryWriter(new FileStream("mydata", FileMode.Create)); //bw.Write(i25);//寫入1個25 // bw.Write(d); // bw.Write(b); bw.Write(s);//寫入一個字符串 bw.Close(); C# 字節數組到字符串 public static string ByteArrayToString(byte[] ba) { string hex = BitConverter.ToString(ba); return hex.Replace("-", ""); } string str = System.Text.Encoding.Default.GetString(byteArray) C# 數組的創建 byte[] bytes = new byte[] { 0x01, 0x02, 0x03 };//直接賦值 byte[] bytes = new byte[10];//每個值為0 byte[] bytes = { }; C# 讀取二進制文件 BinaryReader br = new BinaryReader(new FileStream("mydata.pdf", FileMode.Open)); // var A0 =br.ReadByte(); //讀取一個字節(第一個FF(25)(10進制)37) byte[] bytes = new byte[1000];//每個值為0 for (int i = 0; i < bytes.Length;i++ ) //循環讀取多個字節 { bytes[i] = br.ReadByte(); } //讀取1000字節 byte[] bytes = br.ReadBytes(1000); C# 讀取二進制文件,從指定位置讀取, 和讀取到最后 br.BaseStream.Seek(6236060, SeekOrigin.Begin);// 定位到第6236060個字節 var test = br.BaseStream.Length - br.BaseStream.Position;//總長度-當前位置, 可能是讀取到最后 byte[] bytes = br.ReadBytes((int)test); while (br.BaseStream.Position < br.BaseStream.Length) { // bytes[i] = br.ReadByte(); //讀取到最后 } using (BinaryReader br = new BinaryReader(fs)) { while (br.PeekChar() > -1) { // bytes[i] = br.ReadByte(); //讀取到最后 } }
C# 創建二進制文件並寫入
BinaryWriter bw = new BinaryWriter(new FileStream("mydata", FileMode.Create));
//bw.Write(i25);//寫入1個25
// bw.Write(d);
// bw.Write(b);
bw.Write(s);//寫入一個字符串
bw.Close();
C# 字節數組到字符串
public static string ByteArrayToString(byte[] ba)
{
string hex = BitConverter.ToString(ba);
return hex.Replace("-", "");
}
string str = System.Text.Encoding.Default.GetString(byteArray)
C# 數組的創建
byte[] bytes = new byte[] { 0x01, 0x02, 0x03 };//直接賦值
byte[] bytes = new byte[10];//每個值為0
byte[] bytes = { };
C# 讀取二進制文件
BinaryReader br = new BinaryReader(new FileStream("mydata.pdf", FileMode.Open));
// var A0 =br.ReadByte(); //讀取一個字節(第一個FF(25)(10進制)37)
byte[] bytes = new byte[1000];//每個值為0
for (int i = 0; i < bytes.Length;i++ ) //循環讀取多個字節
{
bytes[i] = br.ReadByte();
}
//讀取1000字節
byte[] bytes = br.ReadBytes(1000);
C# 讀取二進制文件,從指定位置讀取, 和讀取到最后
br.BaseStream.Seek(6236060, SeekOrigin.Begin);// 定位到第6236060個字節
var test = br.BaseStream.Length - br.BaseStream.Position;//總長度-當前位置, 可能是讀取到最后
byte[] bytes = br.ReadBytes((int)test);
while (br.BaseStream.Position < br.BaseStream.Length)
{
// bytes[i] = br.ReadByte(); //讀取到最后
}
using (BinaryReader br = new BinaryReader(fs))
{
while (br.PeekChar() > -1)
{
// bytes[i] = br.ReadByte(); //讀取到最后
}
}