📜 Node 编码 与 解码
Base 64
const 转换为Base64 = Buffer.from("你好,世界").toString("base64");
const Base64解码 = Buffer.from(转换为Base64, "base64").toString();
console.log("转换为Base64 > ", 转换为Base64);
console.log("Base64解码 > ", Base64解码);
1
2
3
4
5
6
2
3
4
5
6
结果
转换为 Base64 > 5L2g5aW977yM5LiW55WM
Base64 解码 > 你好,世界
1
2
2
hex
const 转换为Hex = Buffer.from("你好,世界").toString("hex");
const Hex解码 = Buffer.from(转换为Hex, "hex").toString("utf8");
console.log("转换为Hex > ", 转换为Hex);
console.log("Hex解码 > ", Hex解码);
1
2
3
4
5
6
2
3
4
5
6
结果
转换为Hex > e4bda0e5a5bdefbc8ce4b896e7958c
Hex解码 > 你好,世界
1
2
2
图片
const fs = require("fs");
//编码
function base64_encode(file) {
let bitmap = fs.readFileSync(file);
return Buffer.from(bitmap).toString("base64");
}
//解码
function base64_decode(base64str, file) {
var bitmap = Buffer(base64str, "base64");
fs.writeFileSync(file, bitmap);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15