问题描述
我尝试 UTF-8
转换为的base64
字符串。
的例如:的我有ABCDEF ==在 UTF-8
。这实际上是一个重新presentationA 的base64
字符串。
Example : I have "abcdef==" in UTF-8
. It's in fact a "representation" of a base64
string.
我怎样才能检索ABCDEF ==的base64
字符串(请注意,我不希望有一个ABCDEF ==翻译从 UTF-8
,我要得到一个字符串连接$ C $光盘的base64
其中的是ABCDEF ==)
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==")
修改作为我的问题似乎是不清楚,这里是一个改写:
EDIT As my question seems to be unclear, here is a reformulation :
我的字节数组(比方说,我将其命名为A)重新由的base64
字符串psented $ P $。在的base64
转换给了我ABCDEF ==
My byte array (let's say I name it A) is represented by a base64
string. Convert A in base64
gives me "abcdef=="
这串重新presentation是UTF-8通过套接字发送(注意字符串重新presentation正是在UTF-8和BASE64相同)。所以,我收到包含一个UTF-8的消息无论/ ABCDEF == /不管的UTF-8。
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.
所以,我需要为了从此套接字消息检索的base64abcedf ==字符串获得一个。
So I need to retrieve the base64 "abcedf==" string from this socket message in order to get A.
我希望这是更清晰!
推荐答案
这是一个有点很难说你想什么来实现,但假设你正在试图获得一个Base64字符串时的德codeD 的是 ABCDEF ==
,下面应该工作:
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:
var bytes = Encoding.UTF8.GetBytes("abcdef==");
var base64 = Convert.ToBase64String(bytes);
Console.WriteLine(base64);
这将输出: YWJjZGVmPT0 =
是 ABCDEF ==
连接为Base64 codeD
This will output: YWJjZGVmPT0=
which is abcdef==
encoded in Base64.
编辑:
要取消code Base64的字符串,只需使用 Convert.FromBase64String()
。 E.g.L
To decode a Base64 string, simply use Convert.FromBase64String()
. E.g.L
var base64 = "YWJjZGVmPT0=";
var data = Convert.FromBase64String(base64);
在这一点上,数据将是字节[]
(不是字符串
)。如果我们知道字节数组重新presents在UTF8字符串,那么它可以使用转换回字符串形式:
At this point, data
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:
var str = Encoding.UTF8.GetString(data);
Console.WriteLine(str);
这将输出原始的输入字符串 - ABCDEF ==
在这种情况下
This will output the original input string - abcdef==
in this case.
这篇关于转换UTF-8为base64字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!