introduce
- When opening exe, jpg, and pdf files with Notepad , we will see a lot of garbled characters because binary files contain many characters that cannot be displayed and printed.
- If you want text processing software such as Notepad to process binary data , such as using json to save binary information , you need to first perform a Base64 encoding on , turn all the data into visible characters, and then save it.
- Base64 is a method that uses 64 characters to represent arbitrary binary data . It is often used to transmit small amounts of binary data in URLs, cookies, and web pages.
- Base64 requires converting every three 8Bit bytes into four 6Bit bytes (3*8 = 4*6 = 24), and then adding
two high-order bits 0 to the 6Bit to form four 8Bit bytes, that is Say, the converted string will theoretically be
1/3 longer than the original one.
Example:
(Original) Before conversion: 11111101, 11111110, 11111111 (binary)
Converted to four 6Bits: 111111, 011111, 111011, 111111 (binary)
Pad 0: 00111111, 00011111, 00111011, 00111111 (binary)
Base64 comparison table
The role of Base64
The corresponding Base64 text can be generated and simply encrypted, because the converted text is not the original text. If you need to view the original text, you need to decrypt the Base64 data.
QT interface
coding
QByteArray QByteArray::toBase64() const
QByteArray QByteArray::toBase64(QByteArray::Base64Options options) const
Example:
QByteArray text("Hello world");
text.toBase64();
/* 输出: SGVsbG8gd29ybGQ= */
decoding
interface:
[static] QByteArray QByteArray::fromBase64(const QByteArray &base64)
[static] QByteArray QByteArray::fromBase64(const QByteArray &base64,
QByteArray::Base64Options options)
- Example :
QByteArray::fromBase64("SGVsbG8gd29ybGQ=");
/* 输出: Hello world */
List of Base64Options enumeration values