Nodejs base64编码与解码

1、普通字符串

//编码

new Buffer(String).toString('base64');

//解码

new Buffer(base64Str, 'base64').toString();


2、十六进制Hex

//编码

new Buffer(String, 'base64').toString('hex');

//解码

new Buffer(base64Str, 'hex').toString('utf8');



3、图片

const fs = require('fs');

//编码

function base64_encode(file) {

    let bitmap = fs.readFileSync(file);

    return new Buffer(bitmap).toString('base64');

}

//解码

function base64_decode(base64str, file) {

    var bitmap = new Buffer(base64str, 'base64');

    fs.writeFileSync(file, bitmap);

}


声明:本站所有文章和图片,如无特殊说明,均为原创发布。商业转载请联系作者获得授权,非商业转载请注明出处。
真诚赞赏,手留余香
赞赏
随机推荐
WordPress 数据库操作
WordPress RESTful API 路由代码结构
Linux apt 命令
WordPress 中如何在主题和插件中使用
WordPress 插入文章函数 wp_insert_post()
WordPress 上传附件
JavaScript 中的数据类型自动转换为 Boolean 状态
WordPress 按自定义排序的两种方法