为什么base64编码的字符串的末尾有=符号

本文翻译自:Why does a base64 encoded string have an = sign at the end

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编码,以及如何在C#中计算base64编码,但是我已经看过好几次了,当我将字符串转换为base64时,最后会有一个=

A few questions came up: 提出了一些问题:

  1. Does a base64 string always end with = ? base64字符串是否总是以=结尾吗?
  2. Why does an = get appended at the end? 为什么在末尾添加=

#1楼

参考:https://stackoom.com/question/T1NN/为什么base-编码的字符串的末尾有-符号


#2楼

It's padding. 它是填充。 From http://en.wikipedia.org/wiki/Base64 : http://en.wikipedia.org/wiki/Base64

In theory, the padding character is not needed for decoding, since the number of missing bytes can be calculated from the number of Base64 digits. 从理论上讲,不需要填充字符,因为可以从Base64位数计算出丢失的字节数。 In some implementations, the padding character is mandatory, while for others it is not used. 在某些实现中,填充字符是强制性的,而对于其他实现则不使用。 One case in which padding characters are required is concatenating multiple Base64 encoded files. 需要填充字符的一种情况是串联多个Base64编码的文件。


#3楼

  1. No. 没有。
  2. To pad the Base64-encoded string to a multiple of 4 characters in length, so that it can be decoded correctly. 将Base64编码的字符串的长度填充为4个字符的倍数,以便可以正确对其进行解码。

#4楼

http://www.hcidata.info/base64.htm http://www.hcidata.info/base64.htm

Encoding "Mary had" to Base 64 将“ Mary had”编码为Base 64

In this example we are using a simple text string ("Mary had") but the principle holds no matter what the data is (eg graphics file). 在此示例中,我们使用一个简单的文本字符串(“ Mary had”),但是无论数据是什么(例如图形文件),该原理都成立。 To convert each 24 bits of input data to 32 bits of output, Base 64 encoding splits the 24 bits into 4 chunks of 6 bits. 为了将输入数据的每24位转换为32位输出,Base 64编码将24位拆分为4个6位的块。 The first problem we notice is that "Mary had" is not a multiple of 3 bytes - it is 8 bytes long. 我们注意到的第一个问题是“ Mary have”不是3字节的倍数-它是8字节长。 Because of this, the last group of bits is only 4 bits long. 因此,最后一组位只有4位长。 To remedy this we add two extra bits of '0' and remember this fact by putting a '=' at the end. 为了解决这个问题,我们添加了两个额外的位“ 0”,并在末尾添加“ =”来记住这一事实。 If the text string to be converted to Base 64 was 7 bytes long, the last group would have had 2 bits. 如果要转换为Base 64的文本字符串长7个字节,则最后一组将有2位。 In this case we would have added four extra bits of '0' and remember this fact by putting '==' at the end. 在这种情况下,我们将添加四个额外的位“ 0”,并通过在最后放置“ ==”来记住这一事实。


#5楼

1-No 1-不

2- As a short answer : The 65th character ("=" sign) is used only as a complement in the final process of encoding a message. 2-简短的回答:第65个字符(“ =”符号)仅在消息编码的最终过程中用作补充。

You will not have a '=' sign if your string has a multiple of 3 characters number, because Base64 encoding takes each three bytes (8bits) and represents them as four printable characters in the ASCII standard. 如果您的字符串具有3个字符的倍数,则不会有'='符号,因为Base64编码每三个字节(8位)占用一个字节,并将它们表示为ASCII标准中的四个可打印字符。

Details : 细节 :

(a) If you want to encode (a)如果要编码

ABCDEFG <=> [ ABC ] [ DEF ] [ G ABCDEFG <=> [ ABC ] [ DEF ] [ G

Base64 will deal(producing 4 characters) with the first block 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 space) Base64将处理(产生4个字符)第一个块和第二个块(因为它们完成了),但是对于第三个块,它将在输出中添加一个double ==以完成所需的4个字符,因此结果将是QUJD REVG Rw == (无空格)

(b) If you want to encode... (b)如果您想编码...

ABCDEFGH <=> [ ABC ] [ DEF ] [ GH ABCDEFGH <=> [ ABC ] [ DEF ] [ GH

Similarly, it will add just a single = in the end of the output to get 4 characters the result will be QUJD REVG R0g= (without space) 同样,它将在输出末尾仅添加一个=以获得4个字符,结果将为QUJD REVG R0g = (无空格)


#6楼

It serves as padding . 它用作填充

A more complete answer is that a base64 encoded string doesn't always end with a = , it will only end with one or two = if they are required to pad the string out to the proper length. 一个更完整的答案是,如果要求使用base64编码的字符串将字符串填充到适当的长度,则它并不总是=结尾,而是仅以一个或两个=结尾。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Base64 编码是一种将二进制数据编码字符串的方法,常用于在网络上传输或存储数据。它使用64种字符来表示二进制数据,这些字符包括大写字母、小写字母、数字以及一些特殊字符。 与十六进制编码相比,Base64 编码的优势在于它可以表示任意二进制数据,而十六进制编码只能表示整数。此外,Base64 编码后的字符串也更短,因为它使用了更少的字符来表示二进制数据。 对于文本数据,Base64 编码后的字符串中只包含可打印字符,因此更容易阅读和处理。 另外,在不同的应用中,Base64 编码也有不同的用途。例如,在电子邮件中,它可用于在邮件正文和附件中传输二进制数据;在 HTTP 协议中,它可用于在请求和响应头中传输二进制数据。 ### 回答2: 十六进制字符串Base64编码都是常用的数据表示方式,但它们有不同的应用场景和特点。 首先,十六进制字符串是一种用16个字符(0-9,A-F)表示二进制数据的表示方法。它简单直观,便于人类阅读和理解,适用于某些特定的应用场景,比如显示内存地址、计算机中的数据传输和调试等。然而,十六进制字符串的表示方式需要更多的字符来表达相同的二进制数据,使其相对于二进制数据来说冗余度较高。 相比之下,Base64编码则是一种将二进制数据转换成ASCII字符的编码方式。它使用64个字符(包括大小写字母、数字和一些特殊字符)来表示二进制数据,不仅可以节省存储空间,而且还可以在文本协议中传输二进制数据。Base64编码常见于电子邮件传输、URL参数传递、图片传输等场景。它可以解决文本和二进制之间的转换问题,提高数据在不同系统和环境中的兼容性与可传输性。 因此,虽然十六进制字符串Base64编码都是表示二进制数据的方法,但它们有不同的应用场景和特点,具体选择哪种编码方式需要根据具体的需求和使用环境来确定。 ### 回答3: 十六进制字符串Base64编码是两种不同的编码方式,分别用于不同的场景。 首先,十六进制是一种用16个不同的字符来表示数字的方法,它包含了数字0-9和字母A-F,共16个字符。十六进制字符串主要应用于计算机领域,用于表示二进制数据,如内存地址、文件指纹等。由于它的表示方式较为简单直观,方便机器进行处理和计算,因此在计算机领域广泛使用。 而Base64编码是一种将二进制数据转换为可打印字符的编码方式。它使用64个字符(包括大小写字母、数字和两个特殊字符)来表示二进制数据,通过将每个6个比特位的二进制数据转换成相应的可打印字符来实现编码Base64编码主要用于数据传输、存储和表示,它可以将二进制数据转换成纯文本形式,以便在不支持二进制的系统中传输或保存数据。 为什么在某些情况下要将十六进制字符串转换为Base64编码呢?一方面,Base64编码后的数据长度相对较短,可以节省存储空间和传输带宽;另一方面,Base64编码后的数据只包含可打印字符,更适合在文本环境中使用,例如作为URL参数传递、存储在文本文件中等。因此,在需要将二进制数据以纯文本形式表示或传输时,可以选择使用Base64编码。 总而言之,十六进制字符串Base64编码是两种编码方式,各有其适用的场景。根据实际需求选择合适的编码方式可以更好地满足数据处理和传输的要求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值