What is base64 encoding used for?

后端 未结 18 926
滥情空心
滥情空心 2020-11-22 09:45

I\'ve heard people talking about \"base 64 encoding\" here and there. What is it used for?

18条回答
  •  耶瑟儿~
    2020-11-22 10:06

    It's a textual encoding of binary data where the resultant text has nothing but letters, numbers and the symbols "+", "/" and "=". It's a convenient way to store/transmit binary data over media that is specifically used for textual data.

    But why Base-64? The two alternatives for converting binary data into text that immediately spring to mind are:

    1. Decimal: store the decimal value of each byte as three numbers: 045 112 101 037 etc. where each byte is represented by 3 bytes. The data bloats three-fold.
    2. Hexadecimal: store the bytes as hex pairs: AC 47 0D 1A etc. where each byte is represented by 2 bytes. The data bloats two-fold.

    Base-64 maps 3 bytes (8 x 3 = 24 bits) in 4 characters that span 6-bits (6 x 4 = 24 bits). The result looks something like "TWFuIGlzIGRpc3Rpb...". Therefore the bloating is only a mere 4/3 = 1.3333333 times the original.

提交回复
热议问题