I have this piece of code:
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
Why?
How can I convert correctly the string to byte[]?
Edit: An example
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);