字符编码 进制转换
时间:2011-01-19 来源:skykang
public string StrToHex(string mStr) //返回处理后的十六进制字符串
{
return BitConverter.ToString(
ASCIIEncoding.Default.GetBytes(mStr)).Replace("-", " ");
} /* StrToHex */
16进制字符串转换为10进制字符串
public string HexToStr(string mHex) // 返回十六进制代表的字符串
{
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 */
字符串转换为宽字符
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;
}