我经常听到人们谈论“64进制编码”。它的用途是什么?


当前回答

我要在这里描述的Base64的用法有点hack。所以如果你不喜欢黑客,请不要继续。

当我发现MySQL的utf8不支持4字节unicode字符时,我遇到了麻烦,因为它使用了3字节版本的utf8。那么我做了什么来支持完整的4字节unicode MySQL的utf8?base64在存储到数据库时编码字符串,在检索时解码字符串。

由于base64编码和解码非常快,上面的工作非常完美。

你需要注意以下几点:

Base64编码多使用33%的存储空间 存储在数据库中的字符串不是人类可读的(您可以将其作为数据库字符串使用基本加密形式的特性出售)。

对于任何不支持unicode的存储引擎,都可以使用上述方法。

其他回答

从http://en.wikipedia.org/wiki/Base64

The term Base64 refers to a specific MIME content transfer encoding. It is also used as a generic term for any similar encoding scheme that encodes binary data by treating it numerically and translating it into a base 64 representation. The particular choice of base is due to the history of character set encoding: one can choose a set of 64 characters that is both part of the subset common to most encodings, and also printable. This combination leaves the data unlikely to be modified in transit through systems, such as email, which were traditionally not 8-bit clean. Base64 can be used in a variety of contexts: Evolution and Thunderbird use Base64 to obfuscate e-mail passwords[1] Base64 can be used to transmit and store text that might otherwise cause delimiter collision Base64 is often used as a quick but insecure shortcut to obscure secrets without incurring the overhead of cryptographic key management Spammers use Base64 to evade basic anti-spamming tools, which often do not decode Base64 and therefore cannot detect keywords in encoded messages. Base64 is used to encode character strings in LDIF files Base64 is sometimes used to embed binary data in an XML file, using a syntax similar to ...... e.g. Firefox's bookmarks.html. Base64 is also used when communicating with government Fiscal Signature printing devices (usually, over serial or parallel ports) to minimize the delay when transferring receipt characters for signing. Base64 is used to encode binary files such as images within scripts, to avoid depending on external files. Can be used to embed raw image data into a CSS property such as background-image.

它基本上是一种用ASCII文本编码任意二进制数据的方法。每3个字节的数据需要4个字符,最后可能还会有一些填充。

基本上,输入的每6位都用64个字符的字母表进行编码。“标准”字母表使用a-z, a-z, 0-9和+和/,用=作为填充字符。有url安全的变体。

维基百科是一个相当好的信息来源。

它用于将任意二进制数据转换为ASCII文本。

例如,电子邮件附件就是通过这种方式发送的。

对Brad所说的进行一点扩展:许多电子邮件和Usenet的传输机制以及其他移动数据的方式都不是“8位干净”的,这意味着标准ascii字符集之外的字符可能在传输中被破坏——例如,0x0D可能被视为回车符,并被转换为回车符和换行符。64进制将所有二进制字符映射为几个标准ascii字母、数字和标点符号,这样它们就不会被打乱。

当我们通过web服务传输大型二进制对象(图像)时,我在实际意义上使用它。因此,当我使用python脚本测试c# web服务时,可以使用一点魔法重新创建二进制对象。

(在python中)

import base64
imageAsBytes = base64.b64decode( dataFromWS )