代码拉取完成,页面将自动刷新
c语言版base64编解码算法实现
Base64 是一种基于 64 个可打印字符来表示二进制数据的表示方法,由于 2^6=64,所以每 6 个比特为一个单元,对应某个可打印字符。
Base64 常用于在通常处理文本数据的场合,表示、传输、存储一些二进制数据,包括 MIME 的电子邮件及 XML 的一些复杂数据。
Base64 编码要求把 3 个 8 位字节(3*8=24)转化为 4 个 6 位的字节(4*6=24),之后在 6 位的前面补两个 0,形成 8 位一个字节的形式。 如果剩下的字符不足 3 个字节,则用 0 填充,输出字符使用 =,因此编码后输出的文本末尾可能会出现 1 或 2 个 =。
为了保证所输出的编码位可读字符,Base64 制定了一个编码表,以便进行统一转换。编码表的大小为 2^6=64,这也是 Base64 名称的由来。
在 Base64 中的可打印字符包括字母 A-Z、a-z、数字 0-9,这样共有 62 个字符,此外两个可打印符号在不同的系统中而不同。
以下是 Base64 编码的基本步骤:
Base64 编码具有以下特点:
码值 | 字符 | 码值 | 字符 | 码值 | 字符 | 码值 | 字符 |
---|---|---|---|---|---|---|---|
0 | A | 16 | Q | 32 | g | 48 | w |
1 | B | 17 | R | 33 | h | 49 | x |
2 | C | 18 | S | 34 | i | 50 | y |
3 | D | 19 | T | 35 | j | 51 | z |
4 | E | 20 | U | 36 | k | 52 | 0 |
5 | F | 21 | V | 37 | l | 53 | 1 |
6 | G | 22 | W | 38 | m | 54 | 2 |
7 | H | 23 | X | 39 | n | 55 | 3 |
8 | I | 24 | Y | 40 | o | 56 | 4 |
9 | J | 25 | Z | 41 | p | 57 | 5 |
10 | K | 26 | a | 42 | q | 58 | 6 |
11 | L | 27 | b | 43 | r | 59 | 7 |
12 | M | 28 | c | 44 | s | 60 | 8 |
13 | N | 29 | d | 45 | t | 61 | 9 |
14 | O | 30 | e | 46 | u | 62 | + |
15 | P | 31 | f | 47 | v | 63 | / |
来举例说,一段引用自托马斯·霍布斯《利维坦》的文本:
Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.
使用 Base64 编码之后变成:
TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=
编码 “Man” 的结果为 TWFu,转换过程如下:
文本 | M | a | n | |
---|---|---|---|---|
ASCII 编码 | 77 | 97 | 110 | |
二进制位 | 01001101 | 01100001 | 01101110 | |
二进制位(重组) | 010011 | 010110 | 000101 | 101110 |
索引 | 19 | 22 | 5 | 46 |
Base64 编码 | T | W | F | u |
$ ./base64
file base64 encode/decode test:
encode(./input.jpg) success
decode(encode(./input.jpg)) => ./output.jpg
check md5:
4b2c00b91c9a38d4f85036f6149fc1ff input.jpg
4b2c00b91c9a38d4f85036f6149fc1ff output.jpg
string base64 encode/decode test:
encode("a") => YQ==
decode("YQ==") => a
encode("ab") => YWI=
decode("YWI=") => ab
encode("abc") => YWJj
decode("YWJj") => abc
encode("abcd") => YWJjZA==
decode("YWJjZA==") => abcd
encode("abcde") => YWJjZGU=
decode("YWJjZGU=") => abcde
encode("abcdef") => YWJjZGVm
decode("YWJjZGVm") => abcdef
encode("abcdefg") => YWJjZGVmZw==
decode("YWJjZGVmZw==") => abcdefg
encode("abcdefgh") => YWJjZGVmZ2g=
decode("YWJjZGVmZ2g=") => abcdefgh
encode("abcdefghi") => YWJjZGVmZ2hp
decode("YWJjZGVmZ2hp") => abcdefghi
Binary buffer base64 encode/decode test:
encode(0xff 0xd8 0xff 0xe0 0 0x10 0x4a 0x46 0x49 0x46 0 0x1 0x1 0x1 0 0x7d ) => /9j/4AAQSkZJRgABAQEAfQ==
decode("/9j/4AAQSkZJRgABAQEAfQ==") => 0xff 0xd8 0xff 0xe0 0 0x10 0x4a 0x46 0x49 0x46 0 0x1 0x1 0x1 0 0x7d
==60836== Memcheck, a memory error detector
==60836== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==60836== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==60836== Command: ./base64
==60836==
==60836==
==60836== HEAP SUMMARY:
==60836== in use at exit: 0 bytes in 0 blocks
==60836== total heap usage: 14,628 allocs, 14,628 frees, 213,915 bytes allocated
==60836==
==60836== All heap blocks were freed -- no leaks are possible
==60836==
==60836== For counts of detected and suppressed errors, rerun with: -v
==60836== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。