将 iso-8859-1 转换为 utf-8 javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27155419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:48:04  来源:igfitidea点击:

Convert iso-8859-1 to utf-8 javascript

javascriptnode.js

提问by MrBinWin

I try to parse a "iso-8859-1" page and save to my DB with utf-8, this is my code:

我尝试解析“iso-8859-1”页面并使用 utf-8 保存到我的数据库,这是我的代码:

var buffer = iconv.encode(data, "iso-8859-1");
data = iconv.decode(buffer, 'utf8');

It doesn't work. All symbols like ? or ? convert to ???

它不起作用。所有的符号都像 ? 或者 ?转换成 ???

How can I save these symbols?

如何保存这些符号?

回答by álvaro González

You need a third-party library for that task. You are using iconv-liteso you need to follow these steps:

你需要一个第三方库来完成这项任务。您正在使用iconv-lite,因此您需要执行以下步骤:

  1. Open input file in binarymode, so JavaScript doesn't assume UTF-8 nor try to convert to its internal encoding:

    var fs = require("fs");
    var input = fs.readFileSync(inputFilePath, {encoding: "binary"});
    
  2. Convert from ISO-8859-1 to Buffer:

    var iconv = require('iconv-lite');
    var output = iconv.decode(input, "ISO-8859-1");
    
  3. Save Buffer to output file:

    fs.writeFileSync(outputFilePath, output);
    
  1. 二进制模式打开输入文件,因此 JavaScript 不会假设 UTF-8 也不会尝试转换为其内部编码:

    var fs = require("fs");
    var input = fs.readFileSync(inputFilePath, {encoding: "binary"});
    
  2. 从 ISO-8859-1 转换为Buffer

    var iconv = require('iconv-lite');
    var output = iconv.decode(input, "ISO-8859-1");
    
  3. 将缓冲区保存到输出文件:

    fs.writeFileSync(outputFilePath, output);
    

If unsure of encoding names, you can test whether a given encoding is supported with encodingExists():

如果不确定编码名称,您可以测试是否支持给定的编码encodingExists()

> iconv.encodingExists("ISO-8859-1");
true

回答by Vitaliy Andrusishyn

It's working for me:

它对我有用:

var tempBuffer = new Buffer(response.body, 'iso-8859-1');
var iconv = new Iconv('ISO-8859-1', 'UTF-8');
var tempBuffer = iconv.convert(tempBuffer);

used 'iconv' module https://github.com/bnoordhuis/node-iconv

使用“iconv”模块https://github.com/bnoordhuis/node-iconv