How to use the encode function from iconv-lite

Find comprehensive JavaScript iconv-lite.encode code examples handpicked from public code repositorys.

iconv-lite.encode is a function used to convert a string from one character encoding to another.

627
628
629
630
631
632
633
634
635
636
st.equal(qs.stringify({ 県: '大阪府', '': '' }, {
    encoder: function (str) {
        if (str.length === 0) {
            return '';
        }
        var buf = iconv.encode(str, 'shiftjis');
        var result = [];
        for (var i = 0; i < buf.length; ++i) {
            result.push(buf.readUInt8(i).toString(16));
        }
fork icon0
star icon0
watch icon0

+ 2 other calls in file

1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
});


// função que insere objeto de registro assistencial no banco de dados Pulsar.
const insertRegistroAssistencial = (obj) => {
  // console.log('INSERINDO REGISTRO ASSISTENCIAL...');
  var winvalor = iconv.encode(obj.valor, 'windows-1252');
  var utfvalor = iconv.decode(Buffer.from(winvalor), 'UTF-8');
  var sql = "INSERT INTO gesthos_assistencial (data, hora, prontuario, atendimento, grupo, item, valor) VALUES ($1, $2, $3, $4, $5, $6, $7)"
  pool.query(sql, [
    obj.data,
fork icon0
star icon0
watch icon1

+ 3 other calls in file

How does iconv-lite.encode work?

iconv-lite.encode is a function that takes two arguments - the string to be encoded and the target encoding format. It returns a Buffer object that represents the encoded string. Internally, iconv-lite.encode uses an encoding engine to convert the string to the target encoding format. The engine uses a conversion table that maps each character from the source encoding to its equivalent in the target encoding. If a character in the source encoding cannot be mapped to the target encoding, it will be replaced with a special replacement character. iconv-lite supports a wide range of character encoding formats, including UTF-8, ISO-8859-1, GBK, and more. The library is particularly useful for handling text data that comes from various sources, such as databases, APIs, and files, that may use different encoding formats.

128
129
130
131
132
133
134
135
136
137
const info = req.params;
const info2 = req.body


// //한글을 euc-kr과 url endoing 하는 함수
// const buffer = iconv.encode(info2.text, 'EUC-KR');
// const textEncoded = escape(buffer.toString('binary'));


const url = 'http://m.missyusa.com/mainpage/boards/board_list.asp?id='
fork icon0
star icon0
watch icon1

+ 6 other calls in file

213
214
215
216
217
218
219
220
221
222
   // const iopath1 = path.join(__dirname, appconfig.sysIniUrl);
    // Info.config.ProType="小型站";
    console.log('授权ini文件路径', iopath);

    let IniData = ini.stringify(Info);
    var iniList = iconv.encode(IniData, 'GB2312');

    fs.writeFileSync(path.resolve(iopath1), iniList, { flag: 'w+', encoding: 'ascii' })
    res.status(200).send("授权成功,重启软件后生效").end();
}
fork icon0
star icon0
watch icon1

Ai Example

1
2
3
4
5
6
const iconv = require("iconv-lite");

const utf8String = "This is a UTF-8 string.";
const isoString = iconv.encode(utf8String, "ISO-8859-1");

console.log(isoString.toString("ISO-8859-1")); // outputs 'This is a UTF-8 string.'

In the example above, the iconv-lite library is first imported. Then, a sample string in UTF-8 encoding is defined. The iconv.encode function is called to convert the string to ISO-8859-1 encoding. The resulting Buffer object is then converted to a string using the toString method and specifying the target encoding. Finally, the converted string is logged to the console.

66
67
68
69
70
71
72
73
74
75
let chunks = [];
const lineCountToSave = 1000; // número de líneas a procesar antes de guardar el progreso
let linesProcessed = 0; // número de líneas procesadas desde la última vez que se guardó el progreso

fileStream.on("data", (chunk) => {
  const buffer = iconv.encode(chunk.toString(), "latin1");
  chunks.push(buffer);
});

fileStream.on("end", async () => {
fork icon0
star icon0
watch icon1

+ 10 other calls in file

1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889


// função que insere objeto de registro assistencial no banco de dados Pulsar.
const insertRegistroAssistencial = (obj) => {
  
  var utfvalor = iconv.decode(Buffer.from(obj.valor), 'win1251');
  // var isovalor = iconv.encode(obj.valor, 'iso-8859-1');
  // var utfvalor = iconv.decode(Buffer.from(isovalor), 'utf8');
  var sql = "INSERT INTO gesthos_assistencial (data, hora, prontuario, atendimento, grupo, item, valor) VALUES ($1, $2, $3, $4, $5, $6, $7)"
  pool.query(sql, [
    obj.data,
fork icon0
star icon0
watch icon1

+ 4 other calls in file

1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
// função que insere objeto de registro assistencial no banco de dados Pulsar.
const insertRegistroAssistencial = (obj) => {


  var decoded1 = iconv.decode(Buffer.from(obj.valor), 'windows-1252');
  var decoded2 = iconv.decode(Buffer.from(obj.valor), 'ISO-8859-1');
  var encode = iconv.encode(obj.valor, 'utf8');
  console.log(obj);


  /*
  console.log('VALOR ASCII: ' + decoded0);
fork icon0
star icon0
watch icon1

+ 4 other calls in file

339
340
341
342
343
344
345
346
347
348
  } else {
    encoding = null;
  }
  if (Buffer.isBuffer(buffer) && !UTF8_RE.test(encoding) && !isUtf8(buffer)) {
    try {
      buffer = iconv.encode(buffer, encoding || 'GB18030');
    } catch (e) {}
  }
  return String(buffer || '');
}
fork icon0
star icon0
watch icon1

36
37
38
39
40
41
42
43
44
    content += kessai.id_customer + "," + kessai.to_pay + "," + kessai.nm_customer_1 + "," + kessai.nm_customer_2 + "," + kessai.telno + "," + kessai.price + "," + kessai.yyyymmdd_kigen + "\r\n"
  });
}

const outFileName = process.env.KESSAI_UP_PATH + "\\kessaiinfo" + common.getTodayTime() + ".csv";
const content_SJIS = iconv.encode(content, "Shift_JIS");
await fs.writeFileSync(outFileName, content_SJIS);

return outFileName;
fork icon0
star icon0
watch icon0

+ 2 other calls in file