Posted on April 20th, under node. JS
Tags:ascii, Buffer, Encoding, node. js, UTF
So how does encode a string to Base64 is node. js? Is there something easy like of base64_encode()
PHP ' s?
node. js ' Being ' JavaScript, have a more logical approach to encoding strings, instead of have thousands of inconsistently Defined global functions.
Here's how you encode normal text to base64 in node. JS:
var=newBuffer(' JavaScript ');
var= b. ToString(' base64 ');
smf2yvnjcmlwda==
And here's how you decode Base64 encoded strings:
var=newBuffer(' smf2yvnjcmlwda== ',' base64 ')
var= b. ToString();
Javascript
If you is interested in the details of how the above examples worked, follow me.
The new Buffer()
constructor requires a number, array or string as the first parameter, and an optional encoding type as the Seco nd parameter. The possible encoding types is ASCII, UTF8, UCS2, base64, binary, and hex; The default being UTF8.
By passing the second parameter, we tell JavaScript, the "the string you see was encoded in this particular format". Notice how we do that in the decoding example.
Once we have the encoded string and we call the toString()
method on the string. If we don ' t pass the encoding type toString()
to, JavaScript assumes we want to convert the object to UTF8 encoded string by de Fault. We can make it convert to other formats by passing the encoding type to toString()
.
Let's encode a base64 encoded string to hex:
var=newBuffer(' smf2yvnjcmlwda== ',' base64 ')
var= b. ToString(' hex ');
4a617661536372697074
Now decode it to something humans can read:
var=newBuffer(' 4a617661536372697074 ',' hex ')
var= b. ToString(' UTF8 ');
Javascript
Once you get the basics of Buffer and encoding, you can use your knowledge of the File System module to encode files to Ba Se64 strings.
In this well-commented example we convert an image to Base64 encoded string, and re-generate a copy of the image from the Base64 encoded string.
VarFs= Require(' FS ');
function to encode file data to Base64 encoded string
functionBase64_encode(File) {
Read binary data
VarBitmap=Fs.Readfilesync(File);
Convert binary data to Base64 encoded string
Return New Buffer(Bitmap).Tostring(' Base64 ');
}
function to create file from Base64 encoded string
functionBase64_decode(Base64str,File) {
Create buffer object from Base64 encoded string, it's important to tell the constructor the string is Base64 Enco Ded
VarBitmap= New Buffer(Base64str, ' Base64 ');
Write buffer to File
Fs.Writefilesync(File,Bitmap);
Consolelog ( ' ******** File created from Base64 encoded string ******** ' }
//convert image to Base64 encoded string
var< Span class= "PLN" > Base64str = Base64_encode ( ' kitten.jpg ' );
console. Logbase64str //convert base64 string back to Image
base64_decode (base64str, ' copy.jpg '
Fascinating?
I am sure you learnt isn't only how to encode and decode Base64, but in many other formats as well. Has fun with encoding!
Ps:utf8 is the superset of ASCII. If you is limited to using characters on the standard 中文版 keyboard, you can use ASCII; If you're dealing with ' exotic ' characters and symbols like?, こんにちは,üdvözöljük etc., use UTF.
Base64 encoding/decoding in node. js