关于目前最新0.6.11版中各种 ”encoding“ 参数,到底有哪些?都能放心用吗?
发布于 8 年前 作者 myy 4999 次浏览 最后一次编辑是 3 年前
3 回复

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 模块

上面那段来自 buffer.js ~

谢谢指教!

我也找到了文档中的,我不明白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.

回到顶部