Node.js 支持的编码列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14551608/
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
List of encodings that Node.js supports
提问by dsign
I need to read a file which is encoded with ISO-8859-1 (also called latin1), something like this:
我需要读取一个用 ISO-8859-1(也称为 latin1)编码的文件,如下所示:
var file_contents = fs.readFileSync("test_data.html", "latin1");
However, Node complains about "latin1" or "ISO-8859-1" not being a valid encoding ("Error: Unknown encoding").
但是,Node 抱怨“latin1”或“ISO-8859-1”不是有效的编码(“错误:未知编码”)。
What encodings does readFileSyncaccept?
readFileSync接受哪些编码?
回答by phihag
The list of encodingsthat node supports natively is rather short:
该编码的列表节点支持本身是相当短的:
- ascii
- base64
- hex
- ucs2/ucs-2/utf16le/utf-16le
- utf8/utf-8
- binary/latin1 (ISO8859-1, latin1 only in node 6.4.0+)
- ASCII码
- base64
- 十六进制
- ucs2/ucs-2/utf16le/utf-16le
- utf8/utf-8
- binary/latin1(ISO8859-1,latin1 仅在节点 6.4.0+ 中)
If you are using an older version than 6.4.0, or don't want to deal with non-Unicode encodings, you can recode the string:
如果您使用的是 6.4.0 之前的版本,或者不想处理非 Unicode 编码,您可以重新编码字符串:
Use iconv-liteto recode files:
使用iconv-lite重新编码文件:
var iconvlite = require('iconv-lite');
var fs = require('fs');
function readFileSync_encoding(filename, encoding) {
var content = fs.readFileSync(filename);
return iconvlite.decode(content, encoding);
}
Alternatively, use iconv:
或者,使用iconv:
var Iconv = require('iconv').Iconv;
var fs = require('fs');
function readFileSync_encoding(filename, encoding) {
var content = fs.readFileSync(filename);
var iconv = new Iconv(encoding, 'UTF-8');
var buffer = iconv.convert(content);
return buffer.toString('utf8');
}
回答by markrboyd
If the above solution does not work for you it is may be possible to obtain the same result with the following pure nodejs code. The above did not work for me and resulted in a compilation exception when running 'npm install iconv' on OSX:
如果上述解决方案对您不起作用,则可以使用以下纯 nodejs 代码获得相同的结果。以上对我不起作用,并在 OSX 上运行“npm install iconv”时导致编译异常:
npm install iconv
npm WARN package.json [email protected] No README.md file found!
npm http GET https://registry.npmjs.org/iconv
npm http 200 https://registry.npmjs.org/iconv
npm http GET https://registry.npmjs.org/iconv/-/iconv-2.0.4.tgz
npm http 200 https://registry.npmjs.org/iconv/-/iconv-2.0.4.tgz
> [email protected] install /Users/markboyd/git/portal/app/node_modules/iconv
> node-gyp rebuild
gyp http GET http://nodejs.org/dist/v0.10.1/node-v0.10.1.tar.gz
gyp http 200 http://nodejs.org/dist/v0.10.1/node-v0.10.1.tar.gz
xcode-select: Error: No Xcode is selected. Use xcode-select -switch <path-to-xcode>, or see the xcode-select manpage (man xcode-select) for further information.
fs.readFileSync() returns a Buffer if no encoding is specified. And Buffer has a toString() method that will convert to UTF8 if no encoding is specified giving you the file's contents. See the nodejs documentation. This worked for me.
如果未指定编码,fs.readFileSync() 将返回一个 Buffer。并且 Buffer 有一个 toString() 方法,如果没有指定编码为您提供文件内容,该方法将转换为 UTF8。请参阅 nodejs 文档。这对我有用。

