如何把System.String对象与Byte Array互相转换?
String转换为Byte Array
// C# to convert a string to a byte array. public static byte[] StrToByteArray(string str) { System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); return encoding.GetBytes(str); }
Byte Array转换为String
// C# to convert a byte array to a string. public static byte[] ByteArrayToStr(string str) { byte[] dBytes = { 1, 2, 3 }; string string1; System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding(); string1 = enc.GetString(dBytes); }
资料来源:
How do I convert a string to a byte array and vica-versa in VB.NET and C#?