ASCII码

参考:https://www.runoob.com/w3cnote/ascii.html
1720491146416
1720491159903
ASCII码作用在于编码,将二进制数编码(一个字节8位的二进制数据)为常用的英文字符和符号并且显示出来。

C语言中对ASCII码的引用

直接将字符串定义char以int类型输出。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
int main()
{
char c;
printf("输入一个字符: ");

// 读取用户输入
scanf("%c", &c);

// %d 显示整数
// %c 显示对应字符
printf("%c 的 ASCII 为 %d", c, c);
return 0;
}

Unicode

unicode编码作为ASCII码的拓展,执行相同的功能,但是同时Unicode需要使用两个字节的数据。

BASE64编码

编码方式
1 字符串的ASCII进行二进制形式编码
2 编码后的二进制字符串进行3字节数据变换4字节数据的过程(变换后的数据进行查表置换),不足3字节的数据使用”=”补位

核心点在于将二进制形式下的3字节数据,转换为每6位一组的4字节数据。

1720321453317

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Base64 编码函数
char *base64_encode(const unsigned char *input, size_t length)
{
//Base64码表
static const char b64chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

char *output = (char *)malloc((length + 2) / 3 * 4 + 1);
if (!output)
return NULL;

char *ptr = output;
//Base64移位进行编码
while (length >= 3) 0011 1111 11
0011 1111
0011 1111
{
//0x3F==0011 1111
*ptr++ = b64chars[(*input >> 2) & 0x3F];
//0x30==0011 0000 0x0F==0000 1111
*ptr++ = b64chars[((*input << 4) & 0x30) | ((*(input + 1) >> 4) & 0x0F)];
//0x3C==0011 1100 0x03==0000 0011
*ptr++ = b64chars[((*(input + 1) << 2) & 0x3C) | ((*(input + 2) >> 6) & 0x03)];
//0x3F==0011 1111
*ptr++ = b64chars[*(input + 2) & 0x3F];

input += 3;

length -= 3;
}
//不足3字节对字符串使用"="补位
if (length > 0)
{
*ptr++ = b64chars[(*input >> 2) & 0x3F];
//1字节情况
if (length == 1)
{
*ptr++ = b64chars[(*input << 4) & 0x30];
*ptr++ = '=';
}
//2字节情况
else
{
*ptr++ = b64chars[((*input << 4) & 0x30) | ((*(input + 1) >> 4) & 0x0F)];
*ptr++ = b64chars[((*(input + 1) << 2) & 0x3C)];
}
"="补位
*ptr++ = '=';
}
*ptr = 0;

return output;
}

int main()
{
const char *input = "Hello, World!";
char *encoded = base64_encode((const unsigned char *)input, strlen(input));
printf("Base64 encoded: %s\n", encoded);
free(encoded);
return 0;
}

引用python的import base64,使用定义的方法可以更快的实现base64编码功能。
python官方文档:https://docs.python.org/zh-cn/3/library/base64.html#
常见使用的Base64接口

1
2
3
4
5
6
7
8
9
10
11
base64.b64encode(s, altchars=None)
对 bytes-like object s 进行 Base64 编码,并返回编码后的 bytes。

base64.b64decode(s, altchars=None, validate=False)
解码 Base64 编码过的 bytes-like object 或 ASCII 字符串 s 并返回解码过的 bytes。

base64.standard_b64encode(s)
编码 bytes-like object s,使用标准 Base64 字母表并返回编码过的 bytes。

base64.standard_b64decode(s)
解码 bytes-like object 或 ASCII 字符串 s,使用标准 Base64 字母表并返回编码过的 bytes。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import base64

custom_chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+_'
string=b'Hello,World!'
#标准编码
stand_encode=base64.standard_b64encode(string)
print('standard_encode:',stand_encode)

print('\n')

#非标准编码
encode = base64.b64encode(string, altchars=custom_chars[:2].encode())
print('changed_encode:',encode)
print('\n')

#标准解码
stand_decode=base64.standard_b64decode(stand_encode)
print('standard_decode:',stand_decode)
print('\n')

#非标准解码
decode = base64.b64decode(encode, altchars=custom_chars[:2].encode())
print('changed_decode:',decode)