如何将字符串转换为base64字节数组,是否有效?

时间:2021-11-09 18:31:24

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的)?谢谢!

5 个解决方案

#1


18  

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字节。

#2


52  

You can use:

您可以使用:

From byte[] to string:

从byte[]字符串:

byte[] array = somebytearray;

byte[]数组= somebytearray;

string result = Convert.ToBase64String(array);

字符串的结果= Convert.ToBase64String(数组);

From string to byte[]:

从字符串到byte[]:

array = Convert.FromBase64String(result);

数组= Convert.FromBase64String(结果);

#3


2  

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 always unicode; it is the encoded bytes that change. Since base-64 is always <128, using unicode in the last part 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字符只需要一个字节。

#4


0  

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。

#5


0  

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.

是的,它将输出一个用base64编码的源字符串UTF-16 little-endian表示。记住,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以下。

#1


18  

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字节。

#2


52  

You can use:

您可以使用:

From byte[] to string:

从byte[]字符串:

byte[] array = somebytearray;

byte[]数组= somebytearray;

string result = Convert.ToBase64String(array);

字符串的结果= Convert.ToBase64String(数组);

From string to byte[]:

从字符串到byte[]:

array = Convert.FromBase64String(result);

数组= Convert.FromBase64String(结果);

#3


2  

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 always unicode; it is the encoded bytes that change. Since base-64 is always <128, using unicode in the last part 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字符只需要一个字节。

#4


0  

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。

#5


0  

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.

是的,它将输出一个用base64编码的源字符串UTF-16 little-endian表示。记住,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以下。