C#将string字符串存储为.txt文件

下面为某工程的部分代码,代码实现的功能是将二进制数据文件中的数据读取放入到red_buf空间中,然后将red_buf中的数据转成int型整数,将int型数据以十六进制字符的形式
写入.txt文本文件中

System.Windows.Forms.FolderBrowserDialog folderIQ = new System.Windows.Forms.FolderBrowserDialog();//输出IQ文件的目录
            if (folderIQ.ShowDialog() == DialogResult.OK)
            {
                textBox4.Text = folderIQ.SelectedPath;

                string IQ_dataSaveFilePath = folderIQ.SelectedPath + "IQ_data.txt";
                string I_dataSaveFilePath = folderIQ.SelectedPath + "I_data.txt";
                string Q_dataSaveFilePath = folderIQ.SelectedPath + "Q_data.txt";

    
                string[] write_buf = new string[1024 * 1024 / 2];
                string[] write_buf1 = new string[1024 * 1024 / 2];

                string[] I_data = new string[1024 * 1024 / 4];
                string[] Q_data = new string[1024 * 1024 / 4];


                FileStream dataFile = new FileStream(SaveFilePath + "net_data.dat", FileMode.Open);  //打开数据文件,读取数据
                dataFile.Read(red_buf, 0, 1024 * 1024 * 2);  //读2MB文件数据
                dataFile.Close();
                for (int j = 0; j < 524288; j++)      //2MB数据转化为int型数据
                {
                    RecvDataInt[j] = System.BitConverter.ToInt32(red_buf, j * 4);//该函数可以将4个Byte转为一个Int32型数据
                    //write_buf[j] = Convert.ToString(RecvDataInt[j], 16);
                    //write_buf[j] = RecvDataInt[j].ToString().PadLeft(8,'0');
                    write_buf[j] = RecvDataInt[j].ToString("X8");  //转成16进制字符串且高位补0
                    write_buf1[j] = write_buf[j].Substring(2, 6);  //去掉高8位
                }
                for (int i = 0; i < 262144; i++)
                {
                    I_data[i] = write_buf1[2 * i];
                    Q_data[i] = write_buf1[2 * i + 1];

                }

                System.IO.File.WriteAllLines(IQ_dataSaveFilePath, write_buf1, Encoding.UTF8);  //存txt文件
                System.IO.File.WriteAllLines(I_dataSaveFilePath, I_data, Encoding.UTF8);
                System.IO.File.WriteAllLines(Q_dataSaveFilePath, Q_data, Encoding.UTF8);


            }

你可能感兴趣的:(软件编程)