Example #1
0
 () => buffer.transcode(null, 'utf8', 'ascii'),
Example #2
0
 () => buffer.transcode(Buffer.from('a'), 'uf8', 'b'),
Example #3
0
const buffer = require('buffer');
const assert = require('assert');
const orig = Buffer.from('těst ☕', 'utf8');

// Test Transcoding
const tests = {
  'latin1': [0x74, 0x3f, 0x73, 0x74, 0x20, 0x3f],
  'ascii': [0x74, 0x3f, 0x73, 0x74, 0x20, 0x3f],
  'ucs2': [0x74, 0x00, 0x1b, 0x01, 0x73,
           0x00, 0x74, 0x00, 0x20, 0x00,
           0x15, 0x26]
};

for (const test in tests) {
  const dest = buffer.transcode(orig, 'utf8', test);
  assert.strictEqual(dest.length, tests[test].length, `utf8->${test} length`);
  for (let n = 0; n < tests[test].length; n++)
    assert.strictEqual(dest[n], tests[test][n], `utf8->${test} char ${n}`);
}

{
  const dest = buffer.transcode(Buffer.from(tests.ucs2), 'ucs2', 'utf8');
  assert.strictEqual(dest.toString(), orig.toString());
}

{
  const utf8 = Buffer.from('€'.repeat(4000), 'utf8');
  const ucs2 = Buffer.from('€'.repeat(4000), 'ucs2');
  const utf8_to_ucs2 = buffer.transcode(utf8, 'utf8', 'ucs2');
  const ucs2_to_utf8 = buffer.transcode(ucs2, 'ucs2', 'utf8');