zoukankan      html  css  js  c++  java
  • 编码转换

     

    C# GB2312 转 UTF-8

    http://www.itzhe.cn/article/20080130/69110.html

    http://blog.chinaunix.net/u1/46538/showart_396923.html

     public void EncryptFile(string strInFileName, string strOutFileName)
                {
                    try
                    {
                        byte[] data = File.ReadAllBytes(strInFileName);
                        StreamWriter sw = new StreamWriter(strOutFileName, false, Encoding.Unicode);
                        char[] buffer2 = Encoding.Unicode.GetChars(data);
                        sw.Write(buffer2);
                        sw.Flush();
                        sw.Close();
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
                public void DecryptFile(string strInFileName, string strOutFileNameV)
                {
                    StreamReader sr = new StreamReader(strInFileName, System.Text.Encoding.Unicode);
                    StreamWriter sw = new StreamWriter(strOutFileNameV, false, System.Text.Encoding.GetEncoding("Shift-JIS"));
                    try
                    {
                        char[] buffer;
                        while (!sr.EndOfStream)
                        {
                            buffer = System.Text.Encoding.GetEncoding("Shift-JIS").GetChars(System.Text.Encoding.Unicode.GetBytes(sr.ReadLine()));
                            sw.WriteLine(buffer);
                        }
                        sw.Flush();
                        sw.Close();
                        sr.Close();
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }

                }

    bool writeunicodecsv()
            {
                StreamReader sr = new StreamReader(@"D:\q.csv", System.Text.Encoding.GetEncoding("Shift-JIS"));
                StreamWriter sw = new StreamWriter(@"D:\qUnicode.csv", false, Encoding.Unicode);
              
      try
                {
                    string strline;
                    string  strWrite;
                    int i = 0;
                    while (!sr.EndOfStream)
                    {
                        i++;
                        strline = sr.ReadLine();
                        strWrite = System.Text.Encoding.Unicode.GetString(System.Text.Encoding.GetEncoding("Shift-JIS").GetBytes (strline));
                        sw.WriteLine(strWrite);

                    }
                    sr.Close();
                    sw.Flush();
                    sw.Close();
                    MessageBox.Show(i.ToString());
                }
                catch (Exception ex)
                {
                    throw ex;
                    MessageBox.Show(ex.Message);
                }
                return true;

            }

            bool readcsv()
            {
                string strReadCsvFile = @"D:\qUnicode.csv";
                string strWriteCsvFile = @"D:\qshiftjis.csv";
                StreamReader sr = new StreamReader(strReadCsvFile, System.Text.Encoding.Unicode);
                StreamWriter sw = new StreamWriter(strWriteCsvFile, false, System.Text.Encoding.GetEncoding("Shift-JIS"));
                try
                {
                    string strline;
                    string strWrite;
                    int i = 0;
                    while (!sr.EndOfStream)
                    {
                        i++;
                        strline = sr.ReadLine();          
                        strWrite = System.Text.Encoding.GetEncoding("Shift-JIS").GetString(System.Text.Encoding.Unicode.GetBytes(strline));
                        sw.WriteLine(strWrite);
                    }
                    sr.Close();
                    sw.Flush();
                    sw.Close();
                }
                catch (Exception ex)
                {
                    throw ex;
                    MessageBox.Show(ex.Message);
                }
                return true;
            }

    antony
    :antony1029@163.com
    :http://antony1029.cnblogs.com
  • 相关阅读:
    DataGridView单元格内容自动匹配下拉显示
    C#中datagridviewz中SelectionMode的四个属性的含义
    Visual Studio效率神器——超级扩展ReSharper安装和破解
    vue优化(1) vuecli3/4 【图片压缩 】||【文件压缩】
    DownValues, UpValues, SubValues, 和OwnValues之间的区别?
    Leonid Shifrin 的书
    python newbie——蒙特卡罗法计算圆周率
    python newbie——PE No.1
    指尖上的数学
    瞎猫碰到死耗子
  • 原文地址:https://www.cnblogs.com/antony1029/p/1295290.html
Copyright © 2011-2022 走看看