You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.6 KiB

2 years ago
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace EasyBL
  4. {
  5. /// <summary>
  6. /// 中文字符轉換工具
  7. /// </summary>
  8. public class ChineseStringUtility
  9. {
  10. private const int LOCALE_SYSTEM_DEFAULT = 0x0800;
  11. private const int LCMAP_SIMPLIFIED_CHINESE = 0x02000000;
  12. private const int LCMAP_TRADITIONAL_CHINESE = 0x04000000;
  13. [DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
  14. private static extern int LCMapString(int Locale, int dwMapFlags, string lpSrcStr, int cchSrc, [Out] string lpDestStr, int cchDest);
  15. /// <summary>
  16. /// 将字符转换成简体中文
  17. /// </summary>
  18. /// <param name="source">输入要转换的字符串</param>
  19. /// <returns>转换完成后的字符串</returns>
  20. public static string ToSimplified(string source)
  21. {
  22. var target = new String(' ', source.Length);
  23. var ret = LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_SIMPLIFIED_CHINESE, source, source.Length, target, source.Length);
  24. return target;
  25. }
  26. /// <summary>
  27. /// 将字符转换为繁体中文
  28. /// </summary>
  29. /// <param name="source">输入要转换的字符串</param>
  30. /// <returns>转换完成后的字符串</returns>
  31. public static string ToTraditional(string source)
  32. {
  33. var target = new String(' ', source.Length);
  34. var ret = LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_TRADITIONAL_CHINESE, source, source.Length, target, source.Length);
  35. return target;
  36. }
  37. }
  38. }