C#中字节数组(byte[])和字符串相互转换 C#中有关string和byte[]转换的问题

转换过程主要使用到System.Text.Encoding命名空间下的类

1. 字符串转换成字节数组byte[]:

string str = "This is test string";
byte[] byteArray = System.Text.Encoding.Default.GetBytes(str);

2.字节数组换成字符串:

byte[] byteArray = 通过某种方式获取到的字节数组
string str = System.Text.Encoding.Default.GetString(byteArray);

 如果需要其他编码可以使用如:System.Text.UTF8Encoding class、System.Text.UnicodeEncoding class等

参考: