Module 基礎模塊

模塊iconv

iconv 編碼與解碼模塊

引用方式:

1 2
var encoding = require('encoding'); var iconv = encoding.iconv;

或者

1
var iconv = require('iconv');

iconv 會使用系統已安裝的編碼進行處理,iconv 模塊同時還支持以下內置編碼集:

1 2 3 4 5 6 7 8 9
"utf8", "utf-8", "ucs2", "ucs-2", "utf16", "utf-16", "ucs2le", "ucs-2le", "utf16le", "utf-16le", "ucs2be", "ucs-2be", "utf16be", "utf-16be", "ucs4", "ucs-4", "utf32", "utf-32", "ucs4le", "ucs-4le", "utf32le", "utf-32le", "ucs4be", "ucs-4be", "utf32be", "utf-32be"

靜態函數

encode

用iconv 將文本轉換為二進制數據

1 2
static Buffer iconv.encode(String charset, String data);

調用參數:

  • charset: String, 指定字符集
  • data: String, 要轉換的文本

返回結果:

  • Buffer, 返回解碼的二進制數據

decode

用iconv 將 Buffer 內容轉換為文本

1 2
static String iconv.decode(String charset, Buffer data);

調用參數:

  • charset: String, 指定字符集
  • data: Buffer, 要轉換的二進制數據

返回結果:

  • String, 返回編碼的字符串

isEncoding

檢測字符集是否被支持

1
static Boolean iconv.isEncoding(String charset);

調用參數:

  • charset: String, 指定字符集

返回結果:

  • Boolean, 返回是否支持該字符集