惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Martin Fowler
Martin Fowler
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
IT之家
IT之家
云风的 BLOG
云风的 BLOG
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
Microsoft Azure Blog
Microsoft Azure Blog
Engineering at Meta
Engineering at Meta
P
Proofpoint News Feed
博客园_首页
J
Java Code Geeks
C
Check Point Blog
I
InfoQ
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
D
Docker
U
Unit 42
T
The Blog of Author Tim Ferriss
F
Fortinet All Blogs
GbyAI
GbyAI
N
Netflix TechBlog - Medium
T
Tailwind CSS Blog

Node.js – 魔帆博客

暂无文章

Node.js 使用原生 TextDecoder 类进行编码转换以解决 child_p...
chenjunyu19 · 2017-12-16 · via Node.js – 魔帆博客

前言

由于之前的一个 Windows 下的 Electron 项目需要执行命令来获取一些系统信息,但是由于 child_process.execFileSync() 并不支持 Windows 所使用的 GBK 编码,于是我在网上找到了使用 iconv-lite 来转码的方案。后来,在翻阅 Node.js 文档的时候无意间看到了其实 Node.js 自带编码转换类 TextDecoder。值得注意的是,TextDecoder 需要 Node.js v8.3.0 及更新版本才能使用,如果你的 Node.js 版本过低,那么请及时更新或使用 iconv-lite 吧。

使用方法

其实非常简单,更详细的说明请参阅 Node.js 文档。举个小例子。

const decoder = new TextDecoder('gbk');
console.log(decoder.decode(child_process.execFileSync('TASKLIST')));