Node源码中 有这么一段:
switch (encoding) {
case 'hex':
return this.hexSlice(start, end);
case 'utf8':
case 'utf-8':
return this.utf8Slice(start, end);
case 'ascii':
return this.asciiSlice(start, end);
case 'binary':
return this.binarySlice(start, end);
case 'base64':
return this.base64Slice(start, end);
case 'ucs2':
case 'ucs-2':
return this.ucs2Slice(start, end);
default:
throw new Error('Unknown encoding');
}
不多~, 如果需要更丰富的编码支持,可以安装 iconv 模块
谢谢指教!
我也找到了文档中的,我不明白binary这么有用的方式干嘛要废弃~~
Converting between Buffers and JavaScript string objects requires an explicit encoding method. Here are the different string encodings; 'ascii' - for 7 bit ASCII data only. This encoding method is very fast, and will strip the high bit if set. Note that this encoding converts a null character (‘\0’ or ‘\u0000’) into 0x20 (character code of a space). If you want to convert a null character into 0x00, you should use 'utf8’. 'utf8' - Multi byte encoded Unicode characters. Many web pages and other document formats use UTF-8. 'ucs2' - 2-bytes, little endian encoded Unicode characters. It can encode only BMP(Basic Multilingual Plane, U+0000 - U+FFFF). 'base64' - Base64 string encoding. 'binary' - A way of encoding raw binary data into strings by using only the first 8 bits of each character. This encoding method is deprecated and should be avoided in favor of Buffer objects where possible. This encoding will be removed in future versions of Node. 'hex' - Encode each byte as two hexidecimal characters.