反应本机Base64编码字符串

时间:2017-11-12 23:24:22

标签: react-native base64

我正在尝试使用

中的base-64库

https://github.com/mathiasbynens/base64

当我运行测试来验证代码时,我得不到正确的结果。有没有其他我可以使用的图书馆?

这是我运行的代码和我得到的结果

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=='

以下是我得到的结果

W29iamVjdCBBcnJheUJ1ZmZlcl0 =

有人可以帮忙吗

提前致谢

10 个答案:

答案 0 :(得分:7)

React Native有binaryToBase64 util接受ArrayBuffer进行base64转换:

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==

您可能需要安装utf8软件包,因为版本0.54为removed from React Native

npm install --save utf8

答案 1 :(得分:1)

看看这个包。
这个小型图书馆有2个函数,编码和解码器,都接受字符串。
这可以很好地替代浏览器上的btoa和atob。
https://www.npmjs.com/package/react-native-base64

答案 2 :(得分:1)

您不需要任何额外的库。只需使用:

let encryptedCredentials = new Buffer(username + ":" + password).toString("base64");

答案 3 :(得分:1)

我认为不需要使用任何第三方软件包。 在React-Native中仅次于一个

print(){

        let w=window.open("www.url.com/pdf"); 
        w.print(); 
        w.close();
    }

答案 4 :(得分:0)

您可以使用rn-fetch-blob以本机代码进行转换:

import { fs } from 'rn-fetch-blob';

export const blobToBase64 = async (data, encoding = 'base64') => fs.readFile(data, encoding);

答案 5 :(得分:0)

当我尝试使用Buffer时,我得到了一个错误,表明它已被弃用。我是这样做的:

  const TOKEN = btoa(clientId + ':' + secret);

答案 6 :(得分:0)

可接受的答案和Buffer方法都不适合我。以下是 2019年1月的工作方式:

yarn add react-native-base64

然后

import base64 from 'react-native-base64'
base64.encode('Some string to encode to base64');

答案 7 :(得分:0)

我正在使用base-64utf8

(比上面的rn库更广泛地使用-并且支持utf8字符)

在我们的本机应用程序上进行base64编码/解码。我不明白为什么这必须是本机特定的,它只是javascript,并且可以工作。

npm install base-64 utf8;

然后是我的代码:

    import base64 from 'base-64';
    import utf8 from 'utf8';
    const SALT = 'anyString';
    const PREPENDING_STR = '__enc__';

    function encodeCredential(input) {
        if (input // if the input exists
            && typeof input === 'string' // and it's a string
        ) {
            const newInput = `${input}${SALT}`; // add salt to the input
            const utf8Bytes = utf8.encode(newInput); // utf8 encode it
            const encoded = base64.encode(utf8Bytes); // base64 encode it
            return `${PREPENDING_STR}${encoded}`; // add a prepending string
        }
        return input;
    }

    function decodeCredential(input) {
        if (input // if the input exists
            && typeof input === 'string' // and it's a string
            && input.startsWith(PREPENDING_STR) === true // and it's encoded yet
        ) {
            const newInput = input.replace(PREPENDING_STR, ''); // remove the prepending string
            const utf8Bytes = base64.decode(newInput); // base64 decode it
            const output = utf8.decode(utf8Bytes); // utf8 decode it
            return output.replace(SALT, '');
        }
        return input;
    }

答案 8 :(得分:0)

encodeBase64 = (input) => {  
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';  
let str = input;
let output = '';

for (let block = 0, charCode, i = 0, map = chars;
str.charAt(i | 0) || (map = '=', i % 1);
output += map.charAt(63 & block >> 8 - i % 1 * 8)) {

  charCode = str.charCodeAt(i += 3/4);

  if (charCode > 0xFF) {
    console.log("'btoa' failed: The string to be encoded contains characters outside of the Latin1 range.");
  }

  block = block << 8 | charCode;
}

return output;    

}

答案 9 :(得分:0)

JavaScript中有两个函数分别用于解码和编码base64字符串:

atob()
btoa()
相关问题