[C#] 簡體亂碼轉換

  • 10221
  • 0
  • 2013-07-29

[C#] 簡體亂碼轉換

 

回應問題

http://answers.microsoft.com/zh-hant/windows/forum/windows_7-files/%E6%AD%A4%E5%AD%97%E9%AB%94%E5%A6%82%E4%BD%95/a4cdf4fd-8390-45d4-a11f-95635faf69a6

http://social.technet.microsoft.com/Forums/de-DE/3d32bccd-cae9-467c-8362-20d25c64a36a

 

表單畫面

image

完整程式碼


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private string Big5toGB2312(string strBig5)
        {
            StringBuilder sb = new StringBuilder();
            byte[] unknow = Encoding.GetEncoding("Big5").GetBytes(strBig5);  // 繁體中文 (Big5) 
            return Encoding.GetEncoding("gb2312").GetString(unknow); // 簡體中文 (GB2312) 
        }

        internal const int LOCALE_SYSTEM_DEFAULT = 0x0800;
        internal const int LCMAP_SIMPLIFIED_CHINESE = 0x02000000;
        internal const int LCMAP_TRADITIONAL_CHINESE = 0x04000000;
        [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="strGB2312"></param>
        /// <returns></returns>
        private string GB2312translateBig5(string strGB2312)
        {
            String tTarget = new String(' ', strGB2312.Length);
            int tReturn = LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_TRADITIONAL_CHINESE, strGB2312, strGB2312.Length, tTarget, strGB2312.Length);
            return tTarget;
        }

        private void txtSource_TextChanged(object sender, EventArgs e)
        {
            this.txtGB.Text = Big5toGB2312(this.txtSource.Text);
            this.txtBig5.Text = GB2312translateBig5(this.txtGB.Text);
        }
    }
}

執行結果

image

 

下載

https://dotblogsfile.blob.core.windows.net/user/chou/1306/2013626144654147.zip