C#中byte数组与string类型之间的转换
原文链接:https://blog.csdn.net/weixin_44359158/article/details/116457477
string类型转换为byte[]:
string str = "Test";
byte[] bytTemp = System.Text.Encoding.Default.GetBytes(str);
- 1
- 2
byte[]转换为string
string strTemp = System.BitConverter.ToString(bytTemp);
- 1
但是得到的结果为54-65-73-74,这里需要进行转换
转换代码:
string[] strSplit = strTemp.Split('-');
byte[] bytTemp2 = new byte[strSplit.Length];
for(int i = 0; i < strSplit.Length; i++)
{
bytTemp2[i] = byte.Parse(strSplit[i],System.Globalization.NumberStyles.AllowHexSpecifier);
}
string strResult = System.Text.Encoding.Default.GetString(bytTemp2);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
可以看到strResult = “Test”
标签:转换,string,C#,bytTemp2,System,strSplit,byte From: https://www.cnblogs.com/sunny3158/p/17310973.html