在 C# 中将字节数组转换为字符串并返回

Ris*_*ain 0 c#

我正在将文件读入字节数组并将字节数组转换为字符串以传递到方法中(我无法传递字节数组本身),并且在函数定义中我将字符串重新转换为字节数组。但两个字节数组(转换前后不同)

我正在使用以下试点代码来测试字节数组是否相同。

byte[] bytes = File.ReadAllBytes(@"C:\a.jpg");
 string encoded = Convert.ToBase64String(bytes);
byte[] bytes1 = Encoding.ASCII.GetBytes(encoded);
Run Code Online (Sandbox Code Playgroud)

当我在 api 调用中使用 bytes 时,它会成功,而当我使用 bytes1 时,它会引发异常。请告诉我如何安全地将字节数组转换为字符串并返回,以使两个数组重新排列相同。

Asa*_*Ali 5

用这个:

byte[] bytes = File.ReadAllBytes(@"C:\a.jpg");
string encoded = Convert.ToBase64String(bytes);
byte[] bytes1 = Convert.FromBase64String(encoded);
Run Code Online (Sandbox Code Playgroud)