I am trying to use the base-64 library from
https://github.com/mathiasbynens/base64
When I run a test to validate the code I am not getting the right result. IS there any other library I can use?
Here is the code that I ran and the result I am getting
import utf8 from 'utf8'
import base64 from 'base-64'
var text = 'foo © bar 𝌆 baz';
var bytes = utf8.encode(text);
var encoded = base64.encode(bytes);
console.log(encoded);
// → 'Zm9vIMKpIGJhciDwnYyGIGJheg=='
Here is the result I am getting
W29iamVjdCBBcnJheUJ1ZmZlcl0=
Can some one please help
thanks in advance
If we were to Base64 encode a string we would follow these steps: Take the ASCII value of each character in the string. Calculate the 8-bit binary equivalent of the ASCII values. Convert the 8-bit chunks into chunks of 6 bits by simply re-grouping the digits.
Base-64 maps 3 bytes (8 x 3 = 24 bits) in 4 characters that span 6-bits (6 x 4 = 24 bits). The result looks something like "TWFuIGlzIGRpc3Rpb...".
jpg" const base64_img = base64. encode(json. qr) fs. createFile(file_path, base64_img, 'base64') .
I think dont need to use any third party package for it. Just below one is working in React-Native
const Buffer = require("buffer").Buffer;
let encodedAuth = new Buffer("your text").toString("base64");
React Native has a binaryToBase64 util that accepts ArrayBuffer for base64 conversions:
var utf8 = require('utf8');
var binaryToBase64 = require('binaryToBase64');
var text = 'foo © bar 𝌆 baz';
var bytes = utf8.encode(text);
var encoded = binaryToBase64(bytes);
console.log(encoded);
// Zm9vIMKpIGJhciDwnYyGIGJheg==
You might need to install the utf8
package, since it was removed from React Native on version 0.54:
npm install --save utf8
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With