//定义类
public static class StringByte
{
//将byte转换为16进制的可以阅读的字符串
public static string toString(byte[] bytes)
{
int bytesRec = bytes.Length;
string msg = "";
for (int i = 0; i < bytesRec; i++)
{
msg += String.Format("{0:X} ", bytes[i]);
}
return msg;
}
//将字符串转换为byte[]
public static byte[] toBytes(string str)
{
int leng = str.Length;
if (leng > 0)
{
byte[] re = new byte[leng];
char [] ch = str.ToCharArray();
for (int i = 0; i < leng; i++)
{
re[i] = Convert.ToByte (ch[i]);
}
return re;
}
else
{
return null;
}
}
}
//使用方法
char[] chars = new char[] { '/xff', '/xfe', '/xfd', '/xfa'};
string inputstr = new string(chars);
byte[] bytes = StringByte.toBytes(inputstr);
string view = StringByte.toString(bytes);
MessageBox.Show(view);