[字元轉換]UTF32 常用轉換程式集

[字元轉換]UTF32 常用轉換程式集

看了聖殿祭師: http://www.plurk.com/p/eue5c1 及 璉大: [測試記錄] UTF32 位元組數與 Char 型別

memo些C#常用的轉換程式, 以後就不用東找西找了.

 

   1:          /// <summary>
   2:          /// 10進位轉16進位
   3:          /// </summary>
   4:          /// <param name="ten">16296</param>
   5:          /// <returns>3f98</returns>
   6:          public static string Ten2Hex(int ten){
   7:              return Convert.ToString(ten, 16);
   8:          }
   9:          /// <summary>
  10:          /// 16進位轉10進位
  11:          /// same as Hex2TenUsingNumberStyles(string)
  12:          /// </summary>
  13:          /// <param name="hex">3f98</param>
  14:          /// <returns>16296</returns>
  15:          public static int Hex2Ten(string hex)
  16:          {
  17:              return Convert.ToInt32(hex, 16);
  18:          }
  19:          /// <summary>
  20:          /// 16進位轉10進位
  21:          /// same as Hex2Ten(string)
  22:          /// </summary>
  23:          /// <param name="codePoint">3f98</param>
  24:          /// <returns>16296</returns>
  25:          public static int Hex2TenUsingNumberStyles(string hex)
  26:          {            
  27:              return int.Parse(hex, System.Globalization.NumberStyles.HexNumber);            
  28:          }
  29:   
  30:          /// <summary>
  31:          /// 字串轉10進位代碼
  32:          /// </summary>
  33:          /// <param name="s"></param>
  34:          /// <returns></returns>
  35:          public static int ToUtf32Int(string s)
  36:          {
  37:              return char.ConvertToUtf32(s, 0);
  38:          }
  39:          /// <summary>
  40:          /// 依10進位代碼轉字元
  41:          /// </summary>
  42:          /// <param name="code">16296</param>
  43:          /// <returns>㾨</returns>
  44:          public static string ToUtf32String(int code)
  45:          {   
  46:              return char.ConvertFromUtf32(code);            
  47:          }
  48:          /// <summary>
  49:          /// 依byte[]代碼轉字元
  50:          /// </summary>
  51:          /// <param name="b">[168,63,0,0]</param>
  52:          /// <returns>㾨</returns>
  53:          public static string ToUtf32String(byte[] b)
  54:          {
  55:              return Encoding.UTF32.GetString(b);
  56:          }
  57:          /// <summary>
  58:          /// 不同的編碼轉換為UTF-32字元組
  59:          /// </summary>
  60:          /// <param name="s">㾨 by Big5</param>
  61:          /// <param name="srcEnc">Encoding.Big5</param>
  62:          /// <returns>㾨 by utf32</returns>
  63:          public static string ToUtf32String(string s, Encoding srcEnc)
  64:          {            
  65:              byte[] src = srcEnc.GetBytes(s);
  66:              return Encoding.UTF32.GetString(src);
  67:          }
  68:          /// <summary>
  69:          /// 依字元轉16進位, 無"-"符號
  70:          /// </summary>
  71:          /// <param name="s">㾨</param>
  72:          /// <returns>A83F0000</returns>
  73:          public static string ToUtf32HexString(string s)
  74:          {
  75:              return System.BitConverter.ToString(Encoding.UTF32.GetBytes(s)).Replace("-","");
  76:          }
  77:          /// <summary>
  78:          /// 依字元轉10進位
  79:          /// </summary>
  80:          /// <param name="s">㾨</param>
  81:          /// <returns>16296</returns>
  82:          public static int ToUtf32String(string s)
  83:          {
  84:              return System.BitConverter.ToInt32(Encoding.UTF32.GetBytes(s), 0);
  85:          }
  86:   
  87:          /// <summary>
  88:          /// 簡單的單一byte與char轉換(ascii)
  89:          /// </summary>
  90:          /// <param name="b">65</param>
  91:          /// <returns>A</returns>
  92:          public static char SimpleByte2Char(byte b)
  93:          {
  94:              return (char) b; 
  95:          }
  96:   
  97:          /// <summary>
  98:          /// 簡單的單一byte與char轉換(ascii)
  99:          /// </summary>
 100:          /// <param name="b">A</param>
 101:          /// <returns>65</returns>
 102:          public static byte SimpleChar2Byte(char c)
 103:          {
 104:              return (byte) c;
 105:          }