字符编码 进制转换

    字符串的GB2312编码、GBK编码(ASCII码的十六进制字符串,GB2312是对ASCII码的中文扩展)

public string StrToHex(string mStr) //返回处理后的十六进制字符串
    {
    return BitConverter.ToString(
    ASCIIEncoding.Default.GetBytes(mStr)).Replace("-", " ");
    }

 

   字符串的GB2312编码、GBK编码转换为普遍字符串 
 public string HexToStr(string mHex) // 返回十六进制代表的字符串,从10进制转为16进制 
    {
    mHex = mHex.Replace(" ", "");
    if (mHex.Length <= 0) return "";
    byte[] vBytes = new byte[mHex.Length / 2];
    for (int i = 0; i < mHex.Length; i += 2)
    if (!byte.TryParse(mHex.Substring(i, 2), NumberStyles.HexNumber, null, out vBytes[i / 2]))
    vBytes[i / 2] = 0;
    return ASCIIEncoding.Default.GetString(vBytes);
    } /* HexToStr */

   

    字符串的Unicode编码

  public static string StringToUnicode(string s)
    {
        StringBuilder outs = new StringBuilder();
        foreach (char item in s)
        {
                outs.Append(string.Format("\\u{0:x4}", (int)item));
        }
        return outs.ToString();
    }

  

   字符串的UTF8编码

  public static string StringToUtf8(string s)
    {
        string code = "";
        foreach (byte b in Encoding.UTF8.GetBytes(s))
        {
            code += '%' + b.ToString("");
        }
        return code;
    }

    字符串转换为宽字符

     public static string StringToWChar(string s)
    {
        StringBuilder outs = new StringBuilder();
        foreach (char item in s)
        {
            if (IsEncoding(item))//判断是否编码
            {
                outs.Append(string.Format("\\u{0:x4}", (int)item));
            }
            else//不是中文
            {
                outs.Append(item);
            }
        }
        return outs.ToString();
    }

    private static bool IsEncoding(char cha)
    {
        string NonEncodingChats = "abcdefghijklmnopqrstuvwxyz0123456789`!@#$%^&*()_+|-=\\,./;'[]{}:<>?";
        return NonEncodingChats.IndexOf(cha) == -1;
    }

   

  想了解 "ANSI,ASCII,Unicode,UTF8的区别与联系"  点击http://www.cnblogs.com/skykang/archive/2011/11/24/2262072.html

   

你可能感兴趣的:(进制转换)