zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    渗透测试-内网渗透笔记
    渗透测试-信息搜集笔记
    Mysql注入笔记
    XSS漏洞理解
    tomcat服务器配置及加固
    python3基础学习笔记
    http.sys远程代码执行漏洞(MS15-034)
    Map and Set(映射和集合)
    防抖 | 节流
    for循环中的闭包
  • 原文地址:https://www.cnblogs.com/passedbylove/p/10762093.html
Copyright © 2011-2022 走看看