将UTF-8转换为base64字符串。

时间:2022-11-04 20:34:29

I'm trying to convert UTF-8 to base64 string.

我尝试将UTF-8转换为base64字符串。

Example: I have "abcdef==" in UTF-8. It's in fact a "representation" of a base64 string.

例:我在UTF-8中有“abcdef==”。它实际上是一个base64字符串的“表示”。

How can I retrieve a "abcdef==" base64 string (note that I don't want a "abcdef==" "translation" from UTF-8, I want to get a string encoded in base64 which is "abcdef==").

如何检索“abcdef==”base64字符串(注意,我不想要“abcdef==”“从UTF-8翻译”,我想要得到一个编码在base64中的字符串,它是“abcdef==”)。

EDIT
As my question seems to be unclear, here is a reformulation:

编辑我的问题似乎不清楚,这里是一个重新表述:

My byte array (let's say I name it A) is represented by a base64 string. Converting A to base64 gives me "abcdef==".

我的字节数组(假设我命名为A)用base64字符串表示。将A转换为base64可以得到“abcdef==”。

This string representation is sent through a socket in UTF-8 (note that the string representation is exactly the same in UTF-8 and base64). So I receive an UTF-8 message which contains "whatever/abcdef==/whatever" in UTF-8.

此字符串表示通过UTF-8中的套接字发送(注意,字符串表示在UTF-8和base64中完全相同)。所以我收到一个UTF-8消息,其中包含UTF-8中的“whatever/abcdef==/whatever”。

So I need to retrieve the base64 "abcedf==" string from this socket message in order to get A.

因此,我需要从这个套接字消息中检索base64“abcedf==”字符串,以便得到A。

I hope this is more clear!

我希望这更清楚!

1 个解决方案

#1


128  

It's a little difficult to tell what you're trying to achieve, but assuming you're trying to get a Base64 string that when decoded is abcdef==, the following should work:

要知道你想要达到的目标有点困难,但是假设你想要得到一个Base64字符串,当解码为abcdef==时,下面的操作应该是:

byte[] bytes = Encoding.UTF8.GetBytes("abcdef==");
string base64 = Convert.ToBase64String(bytes);
Console.WriteLine(base64);

This will output: YWJjZGVmPT0= which is abcdef== encoded in Base64.

这将输出:YWJjZGVmPT0=它是abcdef==编码在Base64中。

Edit:

编辑:

To decode a Base64 string, simply use Convert.FromBase64String(). E.g.

要解码Base64字符串,只需使用Convert.FromBase64String()。如。

string base64 = "YWJjZGVmPT0=";
byte[] bytes = Convert.FromBase64String(base64);

At this point, bytes will be a byte[] (not a string). If we know that the byte array represents a string in UTF8, then it can be converted back to the string form using:

此时,字节将是一个字节[](不是字符串)。如果我们知道字节数组表示UTF8中的字符串,那么可以使用以下方法将它转换回字符串形式:

string str = Encoding.UTF8.GetString(bytes);
Console.WriteLine(str);

This will output the original input string, abcdef== in this case.

这将输出原始的输入字符串,abcdef==。

#1


128  

It's a little difficult to tell what you're trying to achieve, but assuming you're trying to get a Base64 string that when decoded is abcdef==, the following should work:

要知道你想要达到的目标有点困难,但是假设你想要得到一个Base64字符串,当解码为abcdef==时,下面的操作应该是:

byte[] bytes = Encoding.UTF8.GetBytes("abcdef==");
string base64 = Convert.ToBase64String(bytes);
Console.WriteLine(base64);

This will output: YWJjZGVmPT0= which is abcdef== encoded in Base64.

这将输出:YWJjZGVmPT0=它是abcdef==编码在Base64中。

Edit:

编辑:

To decode a Base64 string, simply use Convert.FromBase64String(). E.g.

要解码Base64字符串,只需使用Convert.FromBase64String()。如。

string base64 = "YWJjZGVmPT0=";
byte[] bytes = Convert.FromBase64String(base64);

At this point, bytes will be a byte[] (not a string). If we know that the byte array represents a string in UTF8, then it can be converted back to the string form using:

此时,字节将是一个字节[](不是字符串)。如果我们知道字节数组表示UTF8中的字符串,那么可以使用以下方法将它转换回字符串形式:

string str = Encoding.UTF8.GetString(bytes);
Console.WriteLine(str);

This will output the original input string, abcdef== in this case.

这将输出原始的输入字符串,abcdef==。