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);
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM