如何将字符串转换为utf8字节数组,我有以下示例代码:
这样可以正常工作:
StreamWriter file = new StreamWriter(file1, false, Encoding.UTF8);
file.WriteLine(utf8string);
file.Close();
这是错误的,文件是ASCII格式:
byte[] bytes = System.Text.UTF8Encoding.UTF8.GetBytes(utf8string);
FileStream fs = new FileStream(file2, FileMode.CreateNew);
fs.Write(bytes, 0, bytes.Length);
fs.Close();
我想得到这个函数返回的字节数组:
System.IO.File.ReadAllBytes(path_to_file)
因为这样可以正常工作:
byte[] datab = File.ReadAllBytes(file1);
FileStream fs2 = new FileStream(file3, FileMode.CreateNew);
fs2.Write(datab, 0, datab.Length);
fs2.Close();
发布于 2012-07-18 18:55:36
可以再次使用其他选项:
string value = "\u00C4 \uD802\u0033 \u00AE";
byte[] bytes= System.Text.Encoding.UTF8.GetBytes(value);
有关更多信息,请访问Encoding.UTF8 Property
https://stackoverflow.com/questions/11539631
复制相似问题