zoukankan      html  css  js  c++  java
  • C# 写入二进制文件

    写入整型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,6482,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);//写入125

    // 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(); //读取到最后

    }

    }

  • 相关阅读:
    size_type、size_t、differentce_type以及ptrdiff_t
    题目1003:A+B ---c_str(),atoi()函数的使用;remove , erase函数的使用
    字符串中符号的替换---replace的用法
    A+B for Matrices 及 C++ transform的用法
    97.5%准确率的深度学习中文分词(字嵌入+Bi-LSTM+CRF)
    详细解读简单的lstm的实例
    如何使用 Pylint 来规范 Python 代码风格
    Python下Json和Msgpack序列化比较
    除了cPickle,cjson外还有没有更高效点的序列化库了
    python对象序列化或持久化的方法
  • 原文地址:https://www.cnblogs.com/enych/p/9598160.html
Copyright © 2011-2022 走看看