react-native 如何把 gbk 字符串转成 utf8 字符串?

百度不知道
  • 733

尝试使用:

import iconv from 'iconv-lite';

iconv.decode(iconv.encode(str, 'GBK'), 'utf8');

但是报错:

Unable to resolve module `buffer`

请问如何把 gbk 字符串转成 utf8 字符串?

回复
阅读 1.5k
1 个回答
✓ 已被采纳

react native 0.64亲测可用,望采纳~

export function readBlobAsText(blob, encoding) {
    return new Promise((resolve, reject) => {
        const fr = new FileReader();
        fr.onload = () => resolve(fr.result);
        fr.onerror = err => reject(err);

        fr.readAsText(blob, encoding);
    });
}

// 调用方法
const response = await fetch(url, { data });

// fetch后调用
if (response.headers.get('content-type').indexOf('GBK') > 0) {
    const blobTest = await response.blob();
    const text = await readBlobAsText(blobTest, 'GBK');
}
宣传栏