C# 字符串與unicode互相轉換實戰案例

我就廢話不多說瞭,大傢還是直接看代碼吧~

/// <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));

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。如有錯誤或未考慮完全的地方,望不吝賜教。

推薦閱讀: