我将文件读入字节数组,并将字节数组转换为字符串以传递给方法(我不能传递字节数组本身),在函数定义中,我将字符串重新转换为字节数组。但这两个字节数组(转换前后是不同的)
我正在使用下面的试点代码来测试字节数组是否相同。
byte[] bytes = File.ReadAllBytes(@"C:\a.jpg");
string encoded = Convert.ToBase64String(bytes);
byte[] bytes1 = Encoding.ASCII.GetBytes(encoded);当我在api调用中使用字节时,它成功了,而当我使用bytes1时,它抛出了一个异常。请告诉我如何安全地将字节数组转换成字符串,然后再转换回来,这样两个数组才能保持一致。
发布于 2014-05-17 20:17:43
使用以下命令:
byte[] bytes = File.ReadAllBytes(@"C:\a.jpg");
string encoded = Convert.ToBase64String(bytes);
byte[] bytes1 = Convert.FromBase64String(encoded);发布于 2014-05-17 20:21:39
我将从另一个线程发布一个响应:
static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
static string GetString(byte[] bytes)
{
char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}https://stackoverflow.com/questions/23711216
复制相似问题