How can I convert string to utf8 byte array, I have this sample code:
如何将字符串转换为utf8字节数组,我有这个示例代码:
This works ok:
这样可行:
StreamWriter file = new StreamWriter(file1, false, Encoding.UTF8);
file.WriteLine(utf8string);
file.Close();
This works wrong, file is in ASCII:
这工作错误,文件是ASCII:
byte[] bytes = System.Text.UTF8Encoding.UTF8.GetBytes(utf8string);
FileStream fs = new FileStream(file2, FileMode.CreateNew);
fs.Write(bytes, 0, bytes.Length);
fs.Close();
I would like to get byte array what returned by this function:
我想得到这个函数返回的字节数组:
System.IO.File.ReadAllBytes(path_to_file)
because this works ok:
因为这样可行:
byte[] datab = File.ReadAllBytes(file1);
FileStream fs2 = new FileStream(file3, FileMode.CreateNew);
fs2.Write(datab, 0, datab.Length);
fs2.Close();
2 个解决方案
#1
44
Can use other option again:
可以再次使用其他选项:
string value = "\u00C4 \uD802\u0033 \u00AE";
byte[] bytes= System.Text.Encoding.UTF8.GetBytes(value);
For more information can look on Encoding.UTF8 Property
有关更多信息,请查看Encoding.UTF8属性
#2
-6
You just wanted to convert utf8string to bytes, what are these filestreams for?
你只想将utf8string转换为字节,这些文件流是什么?
String ValueToConvert = "Stack overflow";
byte[] ConvertedBytes = ValueToConvert.getBytes("UTF-8");
#1
44
Can use other option again:
可以再次使用其他选项:
string value = "\u00C4 \uD802\u0033 \u00AE";
byte[] bytes= System.Text.Encoding.UTF8.GetBytes(value);
For more information can look on Encoding.UTF8 Property
有关更多信息,请查看Encoding.UTF8属性
#2
-6
You just wanted to convert utf8string to bytes, what are these filestreams for?
你只想将utf8string转换为字节,这些文件流是什么?
String ValueToConvert = "Stack overflow";
byte[] ConvertedBytes = ValueToConvert.getBytes("UTF-8");