加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

C#中字符串的ASCII码转换为字符

(2011-08-19 17:46:28)
标签:

杂谈

分类: C#学习

private void cmdConvert_Click(object sender, EventArgs e)
        {
            string FileName=FileDialog.FileName;

            string NewFileName = BrowserDialog.SelectedPath + "\\" + "JG.txt";
             using (System.IO.StreamReader reader = new System.IO.StreamReader(FileName, Encoding.Default))
             {
                 while (!reader.EndOfStream)
                 {
                     StrList.Add(reader.ReadLine());//将你读取的每行数据写入ArrayList中
                 }
             }
             FileStream fs = new FileStream(NewFileName, FileMode.OpenOrCreate, FileAccess.Write);
             StreamWriter Line = new StreamWriter(fs, System.Text.Encoding.GetEncoding("GB2312"));
             Line.Flush();
             Line.BaseStream.Seek(0, SeekOrigin.Begin);

            ////////////////////////////////add by libiao/////////////////////////////////////
             string[] lines = File.ReadAllLines(FileName);//读出所有行
             for (int i = 0; i < lines.Length; i++)//对每一行进行处理
            {
                string line = lines[i];
                if ((int)line[0] > 127)//判断每一行是否以汉字开头(汉字的ASCII码大于127),若是,则不用转换,直接写入,否则进行转换
                {
                    Line.WriteLine(StrList[i]);//输出
                    continue;//继续读取下一行
                }
                else
                {
                    string convert_no = line.Substring(0, 8);//读前8个数字
                    string convert_no_after = "";
                    //因为.dat文件中的是16进制,而ASCII对应的是10进制,所以首先要将读出的16进制的字符串转换为10进制的整数,
                    //再将整数转换为字节数组,因为最后获得字符的函数的参数是字节数组
                    for (int j = 0; j < 8; )//循环读取8个数字中的每两个数字,单独进行转换
                    {
                        int no = System.Convert.ToInt32(convert_no.Substring(j, 2), 16);//16进制->10进制
                        byte[] Array = System.BitConverter.GetBytes(no); //int->byte[]
                        string sArray = System.Text.Encoding.ASCII.GetString(Array); //byte[]-> ASCII
                        convert_no_after += sArray.Substring(0,1);//看到的分开的那些字符的处理,因为实际转换后的每个字符其后有3个0,去掉它,只要前面第一个字符
                        j = j + 2;//读下一个2位数字
                    }

                    //其他的我没做,大概思路一样
                    string convert_date = line.Substring(8, 8);//读8个数字的日期
                    string convert_hour = line.Substring(16, 2);//读时间
                    string convert_minute = line.Substring(18, 2);//读分钟
                    string convert_pre = line.Substring(20, 4);//读精度
                    string convert_reminder = line.Substring(24);//读余下的

                    line = convert_no_after + convert_date + convert_hour + convert_minute + convert_pre + convert_reminder;
                    StrList[i] = line;//转换后赋值
                }
            //////////////////////////////////////////////////////////////////////////

                Line.WriteLine(StrList[i]);//数据写入新文本文件  
            }
            Line.Flush();
            Line.Close();      
            MessageBox.Show("文件生成成功","文件转换器",MessageBoxButtons.OK, MessageBoxIcon.Information);
            Application.Exit();
        }

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有