public static byte[] StringToByteArray(string str)
{
return Encoding.UTF8.GetBytes(str);
}
public static string ByteArrayToString(byte[] bytes)
{
return Encoding.UTF8.GetString(bytes);
}
本文提供了两个实用的方法:将字符串转换为字节数组和将字节数组转换回字符串,使用UTF-8编码标准。这对于在网络上传输数据或进行文件操作时特别有用。
public static byte[] StringToByteArray(string str)
{
return Encoding.UTF8.GetBytes(str);
}
public static string ByteArrayToString(byte[] bytes)
{
return Encoding.UTF8.GetString(bytes);
}