nodejs base64 encoding and decoding
Normal string
coding
var b = new Buffer('JavaScript'); var s = ('base64'); // SmF2YVNjcmlwdA==
decoding:
var b = new Buffer('SmF2YVNjcmlwdA==', 'base64') var s = (); // JavaScript
Encoding and decoding and converting to hex
var b = new Buffer('SmF2YVNjcmlwdA==', 'base64') var s = ('hex'); // 4a617661536372697074 var b = new Buffer('4a617661536372697074', 'hex') var s = ('utf8'); // JavaScript
Encoding and decoding pictures
var fs = require('fs'); // function to encode file data to base64 encoded string function base64_encode(file) { // read binary data var bitmap = (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 (file, bitmap); ('******** File created from base64 encoded string ********'); } // convert image to base64 encoded string var base64str = base64_encode(''); (base64str); // convert base64 string back to image base64_decode(base64str, '');
nodejs operation base64
node encode string image base64 encoding and decoding.
/* Base64 for normal strings */ var b = new Buffer('abceAdf123'); var s = ('base64'); (s); // YWJjZUFkZjEyMw== let str = "7b3a51166a197c983519096085cebc70d2710146a67691937b2bd3efea91c6f6"; let s1 = new Buffer(str).toString('base64'); // N2IzYTUxMTY2YTE5N2M5ODM1MTkwOTYwODVjZWJjNzBkMjcxMDE0NmE2NzY5MTkzN2IyYmQzZWZlYTkxYzZmNg== (s1); /* Decode base64 */ var b = new Buffer('YWJjZUFkZjEyMw==', 'base64') var s2 = ('utf8'); // hex => Convert to hexadecimal (s2); // abceAdf123 // utf-8 var b = new Buffer('N2IzYTUxMTY2YTE5N2M5ODM1MTkwOTYwODVjZWJjNzBkMjcxMDE0NmE2NzY5MTkzN2IyYmQzZWZlYTkxYzZmNg==', 'base64') var s3 = ('utf8'); (s3); /*Base64 for pictures*/ var fs = require('fs'); function base64_encode(file) { var bitmap = (file); return new Buffer(bitmap).toString('base64'); } function base64_decode(base64str, file) { var bitmap = new Buffer(base64str, 'base64'); (file, bitmap); } var base64str = base64_encode(''); (base64str); base64_decode(base64str, '');
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.