Javascript 修复typescript中错误的文本编码

Javascript 修复typescript中错误的文本编码,javascript,typescript,character-encoding,iconv,Javascript,Typescript,Character Encoding,Iconv,我有一个角度应用程序,它使用输入元素接收和读取文件 有时文件不是用UTF编码的 现在Ts和Js使用utf,所以当utf和我的组件接收到一个乱七八糟的字符串对象时,它们对文本进行解码 我想将其转换为正确的编码,为此我尝试使用iconv lite: 我尝试的内容(为演示目的创建场景的示例代码)- (一) 不幸的是,我似乎无法得到正确的结果 有没有办法使用错误转换的文本重新创建正确的文本字符串 //create misconverted string let hebTestBuffer=ICONV.

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

有时文件不是用UTF编码的

现在Ts和Js使用utf,所以当utf和我的组件接收到一个乱七八糟的字符串对象时,它们对文本进行解码

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

我尝试的内容(为演示目的创建场景的示例代码)-

(一)

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

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

    //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('אבג','win1255');
    let hebTeststr=ICONV.decode(hebTestBuffer,'utf8');
    console.log('buffer:%o',hebTestBuffer);
    console.log(hebTeststr);
        
    console.log('return to normal:');
    hebTestBuffer=ICONV.encode(hebTeststr,'win1255');
    hebTeststr=ICONV.decode(hebTestBuffer,'win1255');
    console.log('buffer:%o',hebTestBuffer);
    console.log(hebTeststr);