asp教程.net 10进制字符串转换为十六进制字符串
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;
}