我另一个奇怪的问题。
我有这样的代码:
byte[] chks = Get64bitData();
string str = Encoding.UTF8.GetString(chks);
byte[] bts = Encoding.UTF8.GetBytes(str); 方法Get64bitData返回8字节数组,然后数组转换为字符串。然后代码再次将字符串转换为字节数组,但是新数组现在具有16字节!
这是什么类型的地狱,如何避免它?
发布于 2015-09-09 15:57:03
任何随机的byte[]都不能像您看到的那样安全地转换为文本。使用Convert.ToBase64String或BitConverter.ToString将字节数组转换为字符串。
byte[] chks = Get64bitData();
string str = Convert.ToBase64String(chks);
byte[] bts = Convert.FromBase64String(str);或者在SoapHexBinary中使用System.Runtime.Remoting.Metadata.W3cXsd2001类
byte[] chks = Get64bitData();
string str = new SoapHexBinary(chks).ToString();
byte[] bts = SoapHexBinary.Parse(str).Value;https://stackoverflow.com/questions/32484197
复制相似问题