我有这段代码:
byte[] bytes = ...
// Here my bytes.Lenght is 181 (for example)
var str = UTF8Encoding.UTF8.GetString(bytes);
bytes = UTF8Encoding.UTF8.GetBytes(str);
// Here my bytes.Lenght is 189
为什么?
如何正确将字符串转换为 byte[]?
编辑:一个例子
public class Person
{
public string Name { get; set; }
public uint Age { get; set; }
}
...
Person p = new Person { Name = "Mary", Age = 24 };
string str;
byte[] b1, b2;
using (var stream = new MemoryStream())
{
new BinaryFormatter().Serialize(stream, p);
b1 = stream.ToArray();
str = UTF8Encoding.UTF8.GetString(b1);
}
b2 = UTF8Encoding.UTF8.GetBytes(str);