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;
}