Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-native decoded base64 encoded string

I try to decode base64 encoded string token in react native, atob not work and library like js-base64 not resolve the problem.

Someone have a solution ?

like image 832
user3703539 Avatar asked Jan 22 '17 02:01

user3703539


People also ask

How do I decode a base 64 string?

To decode a file with contents that are base64 encoded, you simply provide the path of the file with the --decode flag. As with encoding files, the output will be a very long string of the original file. You may want to output stdout directly to a file.

How do I open base64 PDF in react native?

You can use react-native-pdf package (https://www.npmjs.com/package/react-native-pdf). If you want to show the pdf in your app , this package would be quite helpful as it supports loading PDFs from base64 string for both ios and android .


2 Answers

I find some simple way worked for me, the same api as node.

Install buffer

yarn add buffer

Usage:

console.log(Buffer.from("Hello World").toString('base64'));
console.log(Buffer.from("SGVsbG8gV29ybGQ=", 'base64').toString('ascii'));
like image 163
Geng Jiawen Avatar answered Oct 16 '22 20:10

Geng Jiawen


atob and btoa are not supported in JavascriptCore but works when the app runs under Chrome debugger, because JS code runs in Chrome when debugged. There are many base64 modules. https://github.com/mathiasbynens/base64 works fine for me.

like image 31
Haitao Li Avatar answered Oct 16 '22 20:10

Haitao Li