Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React native Base64 encoding string

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

like image 898
dogwasstar Avatar asked Nov 12 '17 23:11

dogwasstar


People also ask

How do I base encode a string in 64?

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.

How does base64 look like?

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...".

How do I save a base64 image in react native?

jpg" const base64_img = base64. encode(json. qr) fs. createFile(file_path, base64_img, 'base64') .


2 Answers

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");
like image 53
Sanjeev Rao Avatar answered Sep 28 '22 08:09

Sanjeev Rao


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
like image 34
Chirag Jain Avatar answered Sep 28 '22 06:09

Chirag Jain