为什么 base64 编码的字符串末尾有一个 = 符号 [英] Why does a base64 encoded string have an = sign at the end
问题描述
我知道base64
编码是什么以及如何在C# 中计算base64
编码,但是我已经多次看到当我将字符串转换为base64 时,有一个=
最后.
I know what base64
encoding is and how to calculate base64
encoding in C#, however I have seen several times that when I convert a string into base64, there is an =
at the end.
出现了几个问题:
base64
字符串总是以=
结尾吗?- 为什么要在末尾附加
=
?
- Does a
base64
string always end with=
? - Why does an
=
get appended at the end?
推荐答案
Q base64 字符串是否总是以 = 结尾?
A: 没有.(USB 这个词是 base64 编码成 dXNi)
A: No. (the word USB is base64 encoded into dXNi)
Q 为什么在末尾附加一个 =?
A:简而言之:最后一个字符(="符号)仅作为补充(填充)在用特殊数量的字符编码消息的最终过程中添加.
A: As a short answer: The last character ("=" sign) is added only as a complement(padding) in the final process of encoding a message with a special number of characters.
如果您的字符串是 3 个字符的倍数,则您将不会有 '=' 符号,因为 Base64
编码需要每个 三个 字节(一个字符 = 1 个字节)) 并将它们表示为 四个 ASCII 标准中的可打印字符.
You will not have a '=' sign if your string has a multiple of 3 characters number, because Base64
encoding takes each three bytes (a character=1 byte) and represents them as four printable characters in the ASCII standard.
示例:
(a)如果你想编码
ABCDEFG <=>[ABC
] [DEF
] [G
Base64
将处理第一个块(产生 4 个字符)和第二个块(因为它们是完整的).但是对于第三个,它会在输出中添加一个双 ==
以完成 4 个所需的字符.因此,结果将是 QUJD REVG Rw==(没有空格).
Base64
will deal with the first block (producing 4 characters) and the second (as they are complete). But for the third it will add a double ==
in the output in order to complete the 4 needed characters. Thus, the result will be QUJD REVG Rw== (without spaces).
(b) 如果您想对 ABCDEFGH 进行编码 <=>[ABC
] [DEF
] [GH
(b) If you want to encode ABCDEFGH <=> [ABC
] [DEF
] [GH
类似地,它只会在输出的末尾添加一个 =
以获得 4 个字符.
similarly, it will add just a single =
in the end of the output to get 4 characters.
结果将是 QUJD REVG R0g=(没有空格).
The result will be QUJD REVG R0g= (without spaces).
这篇关于为什么 base64 编码的字符串末尾有一个 = 符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!