跪求C#繁体big5转简体GB2312方法C#

解决方案 »

  1.   

    using system;
    using system.IO;
    using system.Text;
    using system.Runtime.InteropServices; 
    //big5繁体转换简体的程序
    public class EncodeTool
    {
     
     [DllImport("kernel32.dll", EntryPoint = "LCMapStringA")]
        public static extern int LCMapString(int Locale, int dwMapFlags, byte[] lpSrcStr, int cchSrc, byte[] lpDestStr, int cchDest);
        
     const int LCMAP_SIMPLIFIED_CHINESE = 0x02000000;
     const int LCMAP_TRADITIONAL_CHINESE = 0x04000000;    public static void Main (String[] args)
        {
      if (args.Length<1)
      {
       Console.WriteLine("请指定路径!");
      }
      String[] files=Directory.GetFiles(args[0]);
      if(args.Length==2)
       files=Directory.GetFiles(args[0],args[1]);
      String dir=args[0]+"\\conv";
      if(!Directory.Exists(dir)){
       Directory.CreateDirectory(dir);
      }
      for(int i=0;i   StreamReader sr = new StreamReader(files[i],Encoding.GetEncoding("big5"));
       String lines=sr.ReadToEnd();
       sr.Close();
       lines=ConvertString(lines);
       //Console.WriteLine(lines);
       StreamWriter sw = new StreamWriter(dir+"\\"+files[i],false,Encoding.GetEncoding("gb2312"));
       sw.WriteLine(lines);
       sw.Close();
       Console.WriteLine("转换 {0} ok!",files[i]);
      }
     } public static String ConvertString(String lines){
      Encoding gb2312 = Encoding.GetEncoding(936);
      byte[] src=gb2312.GetBytes(lines);
      byte[] dest = new byte[src.Length];
      LCMapString(0x0804, LCMAP_SIMPLIFIED_CHINESE, src, -1, dest, src.Length);
      return gb2312.GetString(dest);
     }
    }
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;namespace Common
    {
        /// <summary>
        /// 简繁转换
        /// </summary>
        public static class ChineseConverter
        {
            internal const int LOCALE_SYSTEM_DEFAULT = 0x0800;
            internal const int LCMAP_SIMPLIFIED_CHINESE = 0x02000000;
            internal const int LCMAP_TRADITIONAL_CHINESE = 0x04000000;        /// <summary>
            /// 使用OS的kernel.dll做为简繁转换工具,只要有裝OS就可以使用,不用需额外引用dll,但只能做逐字转换,无法进行词意的转换
            /// <para>所以无法将电脑转成计算机</para>
            /// </summary>
            [DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
            internal static extern int LCMapString(int Locale, int dwMapFlags, string lpSrcStr, int cchSrc, [Out] string lpDestStr, int cchDest);        /// <summary>
            /// 繁体转简体
            /// </summary>
            /// <param name="pSource">要转换的繁体字:體</param>
            /// <returns>转换后的简体字:体</returns>
            public static string ToSimplified(string pSource)
            {
                if (string.IsNullOrEmpty(pSource) || pSource.Length < 1)
                    return "";            String tTarget = new String(' ', pSource.Length);
                int tReturn = LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_SIMPLIFIED_CHINESE, pSource, pSource.Length, tTarget, pSource.Length);
                return tTarget;
            }        /// <summary>
            /// 简体转繁体
            /// </summary>
            /// <param name="pSource">要转换的简体字:体</param>
            /// <returns>转换后的繁体字:體</returns>
            public static string ToTraditional(string pSource)
            {
                if (string.IsNullOrEmpty(pSource) || pSource.Length < 1)
                    return "";            String tTarget = new String(' ', pSource.Length);
                int tReturn = LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_TRADITIONAL_CHINESE, pSource, pSource.Length, tTarget, pSource.Length);
                return tTarget;
            }
        }
    }