字符串与16进制相互转换
protected void Page_Load(object sender, EventArgs e)
{
asciiPass("sy_fnews_comments");
}
private void asciiPass(string s1)//字符串转16进制
{
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
string str = "";
foreach (char a in s1)
{
int intAsciiCode = (int)asciiEncoding.GetBytes(a.ToString())[0];//字母的ASCII码值
str += Convert.ToString(intAsciiCode,16);//10进制转16进制
}
Label1.Text = str;
}
protected void Button1_Click(object sender, EventArgs e)//16进制转字符串
{
int len = Label1.Text.Length / 2;
string[] s = new string[len];
string str = "";
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
string s1 = Label1.Text;
for (int i = 0; i < len; i++)
{
s[i] = s1.Substring(i * 2, 2);
int Str10 = int.Parse(s[i], System.Globalization.NumberStyles.AllowHexSpecifier);//16进制转10进制
byte[] byteArray = new byte[] { (byte)Str10 };
str += asciiEncoding.GetString(byteArray);//ASCII转字符串
}
Label1.Text = str;
}

浙公网安备 33010602011771号