当前位置 : 主页 > 网络编程 > c#编程 >

C# 字符串与unicode互相转换实战案例

来源:互联网 收集:自由互联 发布时间:2021-05-09
我就废话不多说了,大家还是直接看代码吧~ /// summary /// 字符串转Unicode /// /summary /// param name="source"源字符串/param /// returnsUnicode编码后的字符串/returns public static string String2Unicode(string

我就废话不多说了,大家还是直接看代码吧~

/// <summary>
  /// 字符串转Unicode
  /// </summary>
  /// <param name="source">源字符串</param>
  /// <returns>Unicode编码后的字符串</returns>
  public static string String2Unicode(string source)
  {
   var bytes = Encoding.Unicode.GetBytes(source);
   var stringBuilder = new StringBuilder();
   for (var i = 0; i < bytes.Length; i += 2)
   {  
    stringBuilder.AppendFormat("\\u{0:x2}{1:x2}", bytes[i + 1], bytes[i]);
   }
   return stringBuilder.ToString();
  }
  /// <summary> 
  /// 字符串转为UniCode码字符串 
  /// </summary> 
  /// <param name="s"></param> 
  /// <returns></returns> 
  public static string StringToUnicode(string s)
  {
   char[] charbuffers = s.ToCharArray();
   byte[] buffer;
   StringBuilder sb = new StringBuilder();
   for (int i = 0; i < charbuffers.Length; i++)
   {
    buffer = System.Text.Encoding.Unicode.GetBytes(charbuffers[i].ToString());
    sb.Append(String.Format("\\u{0:X2}{1:X2}", buffer[1], buffer[0]));
   }
   return sb.ToString();
  }
  /// <summary> 
  /// Unicode字符串转为正常字符串 
  /// </summary> 
  /// <param name="srcText"></param> 
  /// <returns></returns> 
  public static string UnicodeToString(string srcText)
  {
   string dst = "";
   string src = srcText;
   int len = srcText.Length / 6;
   for (int i = 0; i <= len - 1; i++)
   {
    string str = "";
    str = src.Substring(0, 6).Substring(2);
    src = src.Substring(6);
    byte[] bytes = new byte[2];
    bytes[1] = byte.Parse(int.Parse(str.Substring(0, 2), System.Globalization.NumberStyles.HexNumber).ToString());
    bytes[0] = byte.Parse(int.Parse(str.Substring(2, 2), System.Globalization.NumberStyles.HexNumber).ToString());
    dst += Encoding.Unicode.GetString(bytes);
   }
   return dst;
  }

补充:C# unicode string 转换 codepoint

C# 的string和StringBuilder都支持使用codepoint直接构造字符串。unicode的字符串形式一般都是'\u1234'这种转义模式。 其中‘1234'就是unicode codepoint的16进制形式。

通过计算,可以把这种形式的字符串,直接转化为int32类型的codepoint。

/// <summary>
  /// Get the unicode code point
  /// </summary>
  private static int GetUnicodeCodePoint(char c1, char c2, char c3, char c4)
  {
   return UnicodeCharToInt(c1) * 0x1000 +
     UnicodeCharToInt(c2) * 0x100 +
     UnicodeCharToInt(c3) * 0x10 +
     UnicodeCharToInt(c4);
  }
  /// <summary>
  /// Single unicode char convert to int
  /// </summary>
  private static int UnicodeCharToInt(char c)
  {
   switch (c)
   {
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
     return c - '0';
    case 'a':
    case 'b':
    case 'c':
    case 'd':
    case 'e':
    case 'f':
     return c - 'a' + 10;
    case 'A':
    case 'B':
    case 'C':
    case 'D':
    case 'E':
    case 'F':
     return c - 'A' + 10;
   }
   throw new Exception(string.Format("Unicode char '{0}' error", c));
  }

使用的时候codepoint需要强转为char类型。比如:

StringBuilder sb = new StringBuilder();
sb.Append((char) GetUnicodeCodePoint(c1, c2, c3, c4));

以上为个人经验,希望能给大家一个参考,也希望大家多多支持自由互联。如有错误或未考虑完全的地方,望不吝赐教。

网友评论