LCMapString/LCMapStringEx實現簡體字、繁體字的轉換。


c#環境下想要最小程度不使用第三方庫、程序性能,於是選擇了這個Windows API。

轉載自https://coolong124220.nidbox.com/diary/read/8045380

對應的C#調用申明

     ///
        /// 使用系統 kernel32.dll 進行轉換
        ///
        private const int LocaleSystemDefault = 0x0800;
        private const int LcmapSimplifiedChinese = 0x02000000;
        private const int LcmapTraditionalChinese = 0x04000000;

        [DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern int LCMapString(int locale, int dwMapFlags, string lpSrcStr, int cchSrc,
                                              [Out] string lpDestStr, int cchDest);

        public static string ToSimplified(string argSource)
        {
            var t = new String(' ', argSource.Length);
            LCMapString(LocaleSystemDefault, LcmapSimplifiedChinese, argSource, argSource.Length,t, argSource.Length);
            return t;
        }

        public static string ToTraditional(string argSource)
        {
            var t = new String(' ', argSource.Length);
            LCMapString(LocaleSystemDefault, LcmapTraditionalChinese, argSource, argSource.Length,t, argSource.Length);
            return t;
        }

在測試過程中筆者發現有一些字符是轉換不是了的。比如 '雲'

嘗試使用升級版的函數LCMapStringEx,對應調用申明

  [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        private static extern int LCMapStringEx(
                   string lpLocaleName,        //  LPCWSTR      lpLocaleName
                   uint dwMapFlags,        //  DWORD        dwMapFlags
                   string lpSrcStr,        //  LPCWSTR      lpSrcStr
                   int cchSrc,             //  int          cchSrc
                   [Out]
                 string lpDestStr,           //  LPWSTR       lpDestStr
                   int cchDest,            //  int          cchDest
                   IntPtr lpVersionInformation,    //  LPNLSVERSIONINFO lpVersionInformation
                   IntPtr lpReserved,          //  LPVOID       lpReserved
                   IntPtr sortHandle);         //  LPARAM       sortHandle
     public static string ToSimplifiedEx(string argSource)
        {
            var t = new String(' ', argSource.Length);
            //var t = new StringBuilder(argSource.Length);
            LCMapStringEx("zh-CN", LcmapSimplifiedChinese, argSource, argSource.Length, t, argSource.Length, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
            return t;
        }

     public static string ToTraditionalEx(string argSource)
        {            
            var t = new String(' ', argSource.Length);            
            LCMapStringEx("zh-CN", LcmapTraditionalChinese, argSource, argSource.Length, t, argSource.Length, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);            
            return t.ToString();
        }

起初的預想是錯誤的,並未因為升級api而解決這個問題。

網上也好多朋友反映部分漢字轉換不了。按照目前的知識解決不了這個問題,寫blog存檔備份。

 

LCMapString也是查表,實現原理是一樣的。 使用LCMapString有些字符轉換不了,例如“於”(依賴於系統) LCMapString並不轉換內碼

LCMapString也是查表,實現原理是一樣的。
使用LCMapString有些字符轉換不了,例如“於”(依賴於系統)
LCMapString並不轉換內碼,也不轉換習慣用語。

參考資料:

https://blog.csdn.net/zgl7903/article/details/7762374

https://bbs.csdn.net/topics/190012918

http://bbs.aardio.com/forum.php?mod=viewthread&tid=3630

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM