为什么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: 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
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屋!