《转换篇》Sting和Byte[]

fusio / 2023-09-01 / 原文

string转Byte[]

string str = "Hello World";
byte[] byteArray = Encoding.UTF8.GetBytes(str);

Byte[]转string

参考链接:https://www.python100.com/html/53C7X14UU3EW.html
参考链接:https://blog.csdn.net/godwe/article/details/123000423

// UTF8
string str = "Hello world!";
byte[] bytes = Encoding.UTF8.GetBytes(str);
string result = Encoding.UTF8.GetString(bytes);
// GBK
string str = "Hello world!";
byte[] bytes =  Encoding.GetEncoding("GBK").GetBytes(str);
string result =  Encoding.GetEncoding("GBK").GetString(bytes);