1 编码 btoa
btoa
是一个浏览器提供的全局函数,用于将字符串编码为 Base64 编码的字符串。Base64 编码常用于将二进制数据转换为文本格式,以便在网络上传输或存储。
基本用法
const originalString = 'Hello, World!';
const base64String = btoa(originalString);
console.log(base64String); // 输出: SGVsbG8sIFdvcmxkIQ==
参数
- data: 要编码的字符串。
返回值
- string: 编码后的 Base64 字符串。
注意事项
- 字符集限制:
btoa
只能处理 ASCII 字符。如果输入包含非 ASCII 字符,可能会导致错误或意外的结果。 - 二进制数据:如果需要编码二进制数据,可以先将二进制数据转换为字符串,然后再使用
btoa
。
示例
编码 ASCII 字符串
const originalString = 'Hello, World!';
const base64String = btoa(originalString);
console.log(base64String); // 输出: SGVsbG8sIFdvcmxkIQ==
编码包含非 ASCII 字符的字符串
const originalString = '你好,世界!';
try {
const base64String = btoa(originalString);
console.log(base64String); // 可能会输出错误的结果
} catch (e) {
console.error(e); // 抛出错误
}
编码二进制数据
const binaryData = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]);
const binaryString = String.fromCharCode.apply(null, binaryData);
const base64String = btoa(binaryString);
console.log(base64String); // 输出: SGVsbG8sIFdvcmxkIQ==
兼容性
btoa
是浏览器提供的标准函数,所有现代浏览器都支持它。但在 Node.js 环境中,没有 btoa
函数,需要使用 Buffer
来实现类似的功能:
const originalString = 'Hello, World!';
const base64String = Buffer.from(originalString).toString('base64');
console.log(base64String); // 输出: SGVsbG8sIFdvcmxkIQ==
2 解码 atob
解码 Base64 编码的字符串可以使用 atob
函数:
const base64String = 'SGVsbG8sIFdvcmxkIQ==';
const originalString = atob(base64String);
console.log(originalString); // 输出: Hello, World!
ASCII(American Standard Code for Information Interchange,美国信息交换标准代码)是一种字符编码标准,用于表示文本信息。ASCII 编码使用 7 位二进制数来表示 128 个可能的字符,包括英文字母、数字、标点符号以及一些控制字符。
3 ASCII 字符分类
- 可打印字符(Printable Characters):从 32 到 126 的 ASCII 码,包括空格、字母、数字和标点符号。
- 控制字符(Control Characters):从 0 到 31 的 ASCII 码,以及 127,这些字符通常用于控制设备的行为,而不是显示为可见字符。
ASCII 字符表
以下是一些常见的 ASCII 字符及其对应的十进制和十六进制码:
十进制 | 十六进制 | 字符 | 描述 |
---|---|---|---|
0 | 0x00 | NUL | 空字符 |
7 | 0x07 | BEL | 响铃 |
8 | 0x08 | BS | 退格 |
9 | 0x09 | HT | 水平制表符 |
10 | 0x0A | LF | 换行 |
13 | 0x0D | CR | 回车 |
27 | 0x1B | ESC | 逃逸字符 |
32 | 0x20 | 空格 | |
48 | 0x30 | 0 | 数字 0 |
65 | 0x41 | A | 大写字母 A |
97 | 0x61 | a | 小写字母 a |
126 | 0x7E | ~ | 波浪号 |
127 | 0x7F | DEL | 删除 |
示例
将字符串转换为 ASCII 码
const str = 'Hello, World!';
const asciiCodes = Array.from(str).map(char => char.charCodeAt(0));
console.log(asciiCodes); // 输出: [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]
将 ASCII 码转换回字符串
const asciiCodes = [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33];
const str = String.fromCharCode(...asciiCodes);
console.log(str); // 输出: Hello, World!
使用 btoa
和 atob
处理 ASCII 字符
btoa
和 atob
函数用于 Base64 编码和解码,它们只能处理 ASCII 字符。如果输入包含非 ASCII 字符,可能会导致错误或意外的结果。
编码 ASCII 字符串
const originalString = 'Hello, World!';
const base64String = btoa(originalString);
console.log(base64String); // 输出: SGVsbG8sIFdvcmxkIQ==
解码 Base64 字符串
const base64String = 'SGVsbG8sIFdvcmxkIQ==';
const originalString = atob(base64String);
console.log(originalString); // 输出: Hello, World!
注意事项
- 字符集限制:
btoa
和atob
只能处理 ASCII 字符。如果输入包含非 ASCII 字符,可能会导致错误或意外的结果。 - 二进制数据:如果需要编码二进制数据,可以先将二进制数据转换为字符串,然后再使用
btoa
。
PS:学会了记得,点赞,评论,收藏,分享