zoukankan      html  css  js  c++  java
  • nodejs base64 编码解码

    普通字符串 编码解码:

    var b = new Buffer('JavaScript'); var s = b.toString('base64'); 
    // SmF2YVNjcmlwdA== var b = new Buffer('SmF2YVNjcmlwdA==', 'base64') var s = b.toString(); // JavaScript

      



    编码解码并转成hex

    var b = new Buffer('SmF2YVNjcmlwdA==', 'base64') var s = b.toString('hex'); 
    // 4a617661536372697074 var b = new Buffer('4a617661536372697074', 'hex')
    var s = b.toString('utf8');
    // JavaScript

      



    编码解码图片

    var fs = require('fs'); 
    // function to encode file data to base64 encoded string function base64_encode(file) { 
    // read binary data var bitmap = fs.readFileSync(file);
    // convert binary data to base64 encoded string return new Buffer(bitmap).toString('base64'); { // function to create file from base64 encoded string function base64_decode(base64str, file) {
    // create buffer object from base64 encoded string, it is important to tell the constructor that the string is base64 encoded var bitmap = new Buffer(base64str, 'base64');
    // write buffer to file fs.writeFileSync(file, bitmap); console.log('******** File created from base64 encoded string ********'); } // convert image to base64 encoded string var base64str = base64_encode('kitten.jpg'); console.log(base64str);
    // convert base64 string back to image base64_decode(base64str, 'copy.jpg');

      

  • 相关阅读:
    CSP-S2020总结
    题解-P6687 论如何玩转 Excel 表格
    题解-UVA12995 【Farey Sequence】
    题解-P4159 [SCOI2009] 【迷路】
    题解-SP2916【GSS5
    102. 二叉树的层序遍历
    力扣 160 相交链表 快慢指针 双指针
    3. 无重复字符的最长子串 滑动窗口
    最大连续1的个数 III
    B树和B+树
  • 原文地址:https://www.cnblogs.com/adjk/p/5112296.html
Copyright © 2011-2022 走看看