微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

a2u 解码 API编码 API NodeJS 编码处理模块

程序名称:a2u 解码 API编码 API

授权协议: MIT

操作系统: 跨平台

开发语言: JavaScript

a2u 解码 API编码 API 介绍

由于在NodeJs平台上的缺乏对GBK编码的处理,对于国人来说比较郁闷,故而出现了a2u、iconv-lite等这样的GBK编码处理的模块。

a2u 固然没有 iconv-lite 强大,不过如果你仅需要处理 GB K编码的话,a2u是比iconv-lite更佳的选择,只因为其速度更快、性能更好。

用法

解码 API

var fs = require('fs');
var buf = fs.readFileSync('demo.txt'); //txt's encoding is ANSI, the content is "I(我) love(爱) you(你)."
var a2u = require('a2u');
var str, newBuf;
// Convert from an encoded buffer to js string.
str = a2u.decode(buf);
console.log(str);//I(我) love(爱) you(你).
// If you want convert to buffer with ucs2 encoding, the second arg for method(decode) will be true.
newBuf = a2u.decode(buf, true);
console.log('ANSI buffer : ', buf);
console.log('ucs2 buffer : ', newBuf);
console.log(newBuf.toString('ucs2'));//I(我) love(爱) you(你).

编码 API

var fs = require('fs');
var a2u = require('a2u');
var str = "I(我) love(爱) you(你).";
var buf;
// If string
buf = a2u.encode(str);
console.log('ANSI buffer : ', buf);
// If buffer
buf = a2u.encode( new Buffer(str, 'ucs2') );
console.log('ANSI buffer : ', buf);
// Write to file
fs.writeFileSync('ansi.txt', buf);

a2u 解码 API编码 API 官网

https://www.npmjs.com/package/a2u

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐