修复打字稿中错误的文本编码

问题描述

我有一个 Angular 应用程序,它使用输入元素接收和读取文件。

有时文件不是用 UTF 编码的。

现在 Ts 和 Js 使用 utf,因此它们将文本解码为 utf,我的组件接收到一个乱码字符串对象。

我想将其转换为正确的编码,为此我尝试使用 iconv-lite:

我尝试了什么(为了演示目的创建情境的示例代码)-

1)

//create misconverted string
let hebTestBuffer=ICONV.encode('אבג','win1255');
let hebTeststr=ICONV.decode(hebTestBuffer,'utf8');
console.log('buffer:%o',hebTestBuffer);
console.log(hebTeststr);

//turn it back to normal - the actual code that i need to work on a real misconverted text 
console.log('return to normal:');
hebTestBuffer=ICONV.encode(hebTeststr,'utf8');
hebTeststr=ICONV.decode(hebTestBuffer,'win1255');
console.log('buffer:%o',hebTestBuffer);
console.log(hebTeststr);
let hebTestBuffer=ICONV.encode('אבג',hebTestBuffer);
console.log(hebTeststr);
    
console.log('return to normal:');
hebTestBuffer=ICONV.encode(hebTeststr,'win1255');
hebTeststr=ICONV.decode(hebTestBuffer,hebTestBuffer);
console.log(hebTeststr);

不幸的是,我似乎无法得到正确的结果。

有没有办法使用转换错误的文本重新创建正确的文本字符串?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...