问题描述
我一直在使用react-native的binaryToBase64,但是在新的react-native版本(例如0.62.2)中不存在.
有什么建议代替呢?
I have been using react-native's binaryToBase64, but it doesn't exist in new react-native versions (such as 0.62.2).
Any suggestion on what to use instead?
我尝试了js btoa(),但结果有所不同,知道为什么吗?
I tried js btoa(), but results are different, any idea why?
let output = btoa(input);
let output = btoa(input);
输入:Uint8Array(5)[[0,1,2,3,4]
输出:MCwxLDIsMyw0
input: Uint8Array(5) [0, 1, 2, 3, 4]
output: MCwxLDIsMyw0
输入:Uint8Array(5)[5、6、7、8、9]
输出:NSw2LDcsOCw5
input: Uint8Array(5) [5, 6, 7, 8, 9]
output: NSw2LDcsOCw5
let output = binaryToBase64(input);
let output = binaryToBase64(input);
输入:Uint8Array(5)[[0,1,2,3,4]
输出:AAECAwQ =
input: Uint8Array(5) [0, 1, 2, 3, 4]
output: AAECAwQ=
输入:Uint8Array(5)[5、6、7、8、9]
输出:BQYHCAk =
input: Uint8Array(5) [5, 6, 7, 8, 9]
output: BQYHCAk=
推荐答案
import { Buffer } from 'buffer'
export function toBase64(input) {
return Buffer.from(input, 'utf-8').toString('base64')
}
export function fromBase64(encoded) {
return Buffer.from(encoded, 'base64').toString('utf8')
}
这篇关于可以在react-native中使用的binaryToBase64的替代品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!