How to use the decode function from iconv-lite

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

iconv-lite.decode is a function used to decode a string from a specific character encoding format to Unicode.

195
196
197
198
199
200
201
202
203
204
	}
	else if (!iconv.encodingExists(charset)) {
		Zotero.debug(`Unknown charset ${charset} -- decoding as UTF-8`);
		charset = 'utf8';
	}
	body = iconv.decode(body, charset);
	result.response = body;
	result.responseText = body;
}
else {
fork icon41
star icon97
watch icon14

+ 4 other calls in file

213
214
215
216
217
218
219
220
221
222
      }
    });
  }
});
servers_instances[server].stdout.on('data', (data) => {
  data = iconvlite.decode(data, "utf-8");
  err = errors_parser.checkStringForErrors(data.toString());
  if (err != false) {
    fsock = io.sockets.sockets;
    for (const socket of fsock) {
fork icon11
star icon20
watch icon3

+ 9 other calls in file

How does iconv-lite.decode work?

iconv-lite.decode is a function in the iconv-lite library that decodes a buffer or string of bytes from a specified character encoding into a JavaScript string. The function takes three arguments: input: A buffer or string containing the bytes to decode. encoding: A string specifying the character encoding of the input bytes. options: An optional object specifying additional options for the decoding process, such as whether to strip invalid characters or to use a custom replacement character. When iconv-lite.decode is called, it uses a decoding algorithm to convert the input bytes from the specified encoding into Unicode characters. It returns the resulting string. If an error occurs during the decoding process, an error will be thrown.

120
121
122
123
124
125
126
127
128
129
// parse
var str = body
try {
  debug('parse body')
  str = typeof body !== 'string' && encoding !== null
    ? iconv.decode(body, encoding)
    : body
  req.body = parse(str)
} catch (err) {
  next(createError(400, err, {
fork icon0
star icon0
watch icon0

+ 2 other calls in file

1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880


// 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,
    obj.hora,
fork icon0
star icon0
watch icon1

+ 3 other calls in file

Ai Example

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

const encodedData = Buffer.from("aGVsbG8gd29ybGQ=", "base64"); // 'aGVsbG8gd29ybGQ=' is base64-encoded 'hello world'
const decodedData = iconv.decode(encodedData, "utf-8");

console.log(decodedData); // Output: hello world

In the example above, iconv-lite.decode() is used to decode base64-encoded data into human-readable text using the UTF-8 encoding.

391
392
393
394
395
396
397
398
399
400
});

// 获取response
let data;
if (type === 'iet') {
    data = iconv.decode(response.data, 'gb2312'); // 转码
} else {
    data = response.data;
}
const $ = cheerio.load(data);
fork icon0
star icon0
watch icon1

+ 3 other calls in file

18
19
20
21
22
23
24
25
26
27
        headers: {
            Cookie: cookieString,
        },
    });

    const htmlString = iconv.decode(response.data, 'gbk');
    return cheerio.load(htmlString);
};

const getLastPageId = async (tid, authorId) => {
fork icon0
star icon0
watch icon1

+ 3 other calls in file

4
5
6
7
8
9
10
11
12
13
14
15
const utils = {
    loadDetailPage: async function loadDetailPage(link) {
        const response = await got.get(link, {
            responseType: 'buffer',
        });
        response.data = iconv.decode(response.data, 'gb2312');


        const $ = cheerio.load(response.data);


        const detailInfo = {
fork icon0
star icon0
watch icon1

+ 7 other calls in file

31
32
33
34
35
36
37
38
39
40
            url: itemLink,
            headers: header,
        })
    ).data;
} else {
    responseData = iconv.decode(
        (
            await got({
                method: 'get',
                url: itemLink,
fork icon0
star icon0
watch icon1

+ 7 other calls in file

22
23
24
25
26
27
28
29
30
31
const response = await got({
    method: 'get',
    url: link,
    responseType: 'buffer',
});
const data = iconv.decode(response.data, 'gb2312');
const $ = cheerio.load(data);

let desc = $('#endtext').html();
const next_page_link = $('#pages').children().last().attr('href');
fork icon0
star icon0
watch icon1

+ 7 other calls in file

577
578
579
580
581
582
583
584
585
586
587
 * @param {Uint8Array} body Buffer
 * @returns {string}
 */
exports.convertToUTF8 = (body) => {
    const guessEncoding = chardet.detect(body);
    const str = iconv.decode(body, guessEncoding);
    return str.toString();
};


/**
fork icon0
star icon0
watch icon1

+ 3 other calls in file

537
538
539
540
541
542
543
544
545
546
            var parts = reg.exec(str);
            while (parts) {
                result.push(parseInt(parts[1], 16));
                parts = reg.exec(str);
            }
            return iconv.decode(SaferBuffer.from(result), 'shift_jis').toString();
        }
    }), { 県: '大阪府' });
    st.end();
});
fork icon0
star icon0
watch icon1

+ 4 other calls in file

78
79
80
81
82
83
84
85
86
87
const url = 'http://m.missyusa.com/mainpage/boards/board_list.asp?id='
            + info.sectionid  + '&category=0&key_field=&mypost=0&key_word=&page=' + info.page;

  axios.get(url,{responseType: "arraybuffer"})
    .then((response)=>{
        const contnet = iconv.decode(response.data, "EUC-KR").toString()
        const $ = cheerio.load(contnet)

    titles = [] 
   
fork icon0
star icon0
watch icon1

+ 27 other calls in file

45
46
47
48
49
50
51
52
53
54
    transformResponse: data => {
        const encoding = chardet.detect(data);
        if (!encoding) {
            throw new Error('chardet failed to detect encoding');
        }
        return iconv.decode(data, encoding);
    }
}).catch(err => "error");
const html = typeof res === "string" ? res : res.data;
if (html === "error")
fork icon0
star icon0
watch icon1

+ 7 other calls in file

37
38
39
40
41
42
43
44
45
46
stream.on('data', (chunk) => {
  data += chunk;
});
stream.on('end', () => {
  const buf = Buffer.from(data, 'binary');
  const str = iconv.decode(buf, 'GBK'); // node不支持GBK
  let strArr = str.split('\r\n');
  // 组装对象
  for (let i = 0; i < strArr.length - 1; i++) {
    if (i === 0) { // csv第一列作为对象的key
fork icon0
star icon0
watch icon0

+ 8 other calls in file

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,
    obj.hora,
fork icon0
star icon0
watch icon1

+ 9 other calls in file

1879
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);


  /*
fork icon0
star icon0
watch icon1

+ 9 other calls in file

174
175
176
177
178
179
180
181
182
try {
    // const iopath = path.join(__dirname, appconfig.sysIniUrl); // 引用Pos.ini的相对地址
    const iopath =path.resolve(appconfig.sysIniUrl);
    
    let data = fs.readFileSync(iopath);
    var str = iconv.decode(data, 'GB2312');
    console.log(str);
    const Info = ini.parse(str);
    console.log('修改ini文件路径', iopath);
fork icon0
star icon0
watch icon1

551
552
553
554
555
556
557
558
559
560
    });
});
it('should render ANSI art', function() {
    return Promise.all(['nf-marble.ans', 'bs-pacis.ans'].map(fname => {
        return fs.readFileAsync(`__tests__/${fname}`).then(data => {
            var str = iconv.decode(data, 'CP437');
            return $.terminal.from_ansi(str);
        });
    })).then(data => {
        data.forEach(ansi => {
fork icon0
star icon0
watch icon1

21
22
23
24
25
26
27
28
29
30
  return Promise.resolve(null);
}
let result = stream[text];
if (!result) {
  result = getBody(stream).then((buf) => {
    return buf ? iconv.decode(buf, util.getCharset(stream.headers)) : '';
  });
  stream[text] = result;
}
return result;
fork icon0
star icon0
watch icon1

+ 2 other calls in file

1878
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 decoded = iconv.decode(Buffer.from(obj.valor), 'latin1');
  var encode = iconv.encode(obj.valor, 'utf8');
  
  var cedilla = Buffer.from(obj.valor, 'latin1');
  console.log('CEDILHA: ' + cedilla);
fork icon0
star icon0
watch icon1

+ 4 other calls in file