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();
            }

        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM