C# 字符串到字节数组,字节数组转整型

            int num = 12345;
            string num1 = Convert.ToString(12345, 16);
            byte[] bytes = BitConverter.GetBytes(num);//将int32转换为字节数组
            num = BitConverter.ToInt32(bytes, 0);//将字节数组内容再转成int32类型

 

            string no = DateTime.Now.ToString("yyyyMMddhhmmssfff"); //时间转字符串
            Console.WriteLine(no);

 

        private void button1_Click(object sender, EventArgs e)
        {


            // //字符串到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

            // 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();
            // MessageBox.Show("ccc");
            //reading from the file
            BinaryReader br = new BinaryReader(new FileStream("mydata.pdf", FileMode.Open));
            //var i25 = br.ReadInt32();
            //var d = br.ReadDouble();
            //var b = br.ReadBoolean();

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


       


            //var s1 = br.ReadString();

            MessageBox.Show("ccc");
            string str = System.Text.Encoding.Default.GetString(bytes);
            br.Close();
        }

        public void F1()
        {

            string path = @"C:\a.txt";
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            char cha; int num; double doub; string str;
            try
            {
                while (true)
                {
                    cha = br.ReadChar();//从当前流中读取下一个字符
                    num = br.ReadInt32(); //从当前流中读取4字节有符号整数
                    doub = br.ReadDouble(); //从当前流中读取8字节浮点值
                    str = br.ReadString();//从当前流中读取一个字符串
                    Console.WriteLine("{0},{1},{2},{2}", cha, num, doub, str);
                }
            }
            catch (EndOfStreamException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine("已经读到末尾");
            }
            finally
            {
                Console.ReadKey();
            }

        }

 

posted @ 2018-11-23 11:27  enych  阅读(3151)  评论(0编辑  收藏  举报