C# 如何将字符串转换为 base64 字节数组,这是否有效?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2295408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert string to base64 byte array, would this be valid?
提问by Tomas Vinter
I'm trying to write a function that converts a string to a base64 byte array. I've tried with this approach:
我正在尝试编写一个将字符串转换为 base64 字节数组的函数。我试过这种方法:
public byte[] stringToBase64ByteArray(String input)
{
byte[] ret = System.Text.Encoding.Unicode.GetBytes(input);
string s = Convert.ToBase64String(input);
ret = System.Text.Encoding.Unicode.GetBytes(s);
return ret;
}
Would this function produce a valid result (provided that the string is in unicode)? Thanks!
这个函数会产生一个有效的结果吗(假设字符串是 unicode 的)?谢谢!
采纳答案by Hans Passant
Looks okay, although the approach is strange. But use Encoding.ASCII.GetBytes() to convert the base64 string to byte[]. Base64 encoding only contains ASCII characters. Using Unicode gets you an extra 0 byte for each character.
看起来不错,虽然方法很奇怪。但是使用 Encoding.ASCII.GetBytes() 将 base64 字符串转换为 byte[]。Base64 编码仅包含 ASCII 字符。使用 Unicode 会为每个字符获得一个额外的 0 字节。
回答by Marc Gravell
Representing a string as a blob represented as a string is odd... any reason you can't just use the string directly?
将字符串表示为表示为字符串的 blob 很奇怪……您不能直接使用字符串的任何原因?
The string is alwaysunicode; it is the encoded bytes that change. Since base-64 is always <128, using unicode in the lastpart seems overkill (unless that is what the wire-format demands). Personally, I'd use UTF8 or ASCII for the last GetBytes
so that each base-64 character only takes one byte.
字符串始终是unicode;改变的是编码字节。由于 base-64 总是 <128,因此在最后一部分使用 unicode似乎有点过分(除非这是线格式的要求)。就个人而言,我会在最后使用 UTF8 或 ASCII,GetBytes
这样每个 base-64 字符只占用一个字节。
回答by Darin Dimitrov
All strings in .NET are unicode. This code will produce valid result but the consumer of the BASE64 string should also be unicode enabled.
.NET 中的所有字符串都是 unicode。此代码将产生有效结果,但 BASE64 字符串的使用者也应启用 unicode。
回答by Matteo Italia
Yes, it would output a base64-encoded string of the UTF-16 little-endian representation of your source string. Keep in mind that, AFAIK, it's not really common to use UTF-16 in base64, ASCII or UTF-8 is normally used. However, the important thing here is that the sender and the receiver agree on which encoding must be used.
是的,它会输出源字符串的 UTF-16 little-endian 表示的 base64 编码字符串。请记住,AFAIK,在 base64 中使用 UTF-16 并不常见,通常使用 ASCII 或 UTF-8。然而,这里重要的是发送方和接收方就必须使用哪种编码达成一致。
I don't understand why you reconvert the base64 string in array of bytes: base64 is used to avoid encoding incompatibilities when transmitting, so you should keep is as a string and output it in the format required by the protocol you use to transmit the data. And, as Marc said, it's definitely overkill to use UTF-16 for that purpose, since base64 includes only 64 characters, all under 128.
我不明白你为什么在字节数组中重新转换 base64 字符串:base64 用于避免传输时编码不兼容,因此您应该将 is 保留为字符串并以用于传输数据的协议所需的格式输出. 而且,正如 Marc 所说,为此目的使用 UTF-16 绝对是矫枉过正,因为 base64 仅包含 64 个字符,全部都在 128 以下。
回答by Sonhja
You can use:
您可以使用:
From byte[] to string:
从字节[]到字符串:
byte[] array = somebytearray;
byte[] array = somebytearray;
string result = Convert.ToBase64String(array);
string result = Convert.ToBase64String(array);
From string to byte[]:
从字符串到字节[]:
array = Convert.FromBase64String(result);
array = Convert.FromBase64String(result);