node.js Buffer.toString() 支持哪些编码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34476754/
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-09-02 19:41:41  来源:igfitidea点击:

What encodings does Buffer.toString() support?

node.js

提问by Grayda

I'm writing an app in node.js, and see that I can do things like this:

我正在 node.js 中编写一个应用程序,并看到我可以做这样的事情:

var buf = new Buffer("Hello World!")
console.log(buf.toString("hex"))
console.log(buf.toString("utf8"))

And I know of 'ascii' as an encoding type (it'll take an ASCII code, such as 112and turn it into a p), but what other types of encoding can I do?

而且我知道“ascii”是一种编码类型(它将采用 ASCII 代码,例如112并将其转换为 a p),但是我可以做哪些其他类型的编码?

回答by mscdex

The official node.js documentation for Bufferis the best place to check for something like this. As previously noted, Buffercurrently supports these encodings: ascii, utf8, utf16le/ucs2, base64, binary, and hex.

官方node.js 文档Buffer是检查此类内容的最佳位置。如前所述,Buffer目前支持这些编码:asciiutf8utf16le/ ucs2base64binary,和hex

回答by Grayda

As is always the way, I spent a while Googling but found nothing until after I posted the question:

一如既往,我花了一段时间谷歌搜索,但在我发布问题之前什么也没找到:

http://www.w3resource.com/node.js/nodejs-buffer.phphas the answer. You can use the following types in .toString()on a buffer:

http://www.w3resource.com/node.js/nodejs-buffer.php有答案。您可以在.toString()缓冲区中使用以下类型:

  • ascii
  • utf8
  • utf16le
  • ucs2(alias of utf16le)
  • base64
  • binary
  • hex
  • ascii
  • utf8
  • utf16le
  • ucs2(的别名utf16le
  • base64
  • binary
  • hex

回答by trquoccuong

It support ascii , utf-8 , ucs2, base64, binary

IT支持 ascii , utf-8 , ucs2, base64, binary