Skip to content

Instantly share code, notes, and snippets.

@philpoore
Created August 3, 2017 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philpoore/4c8535814fc4cfb4a2bd7e2f9740f2de to your computer and use it in GitHub Desktop.
Save philpoore/4c8535814fc4cfb4a2bd7e2f9740f2de to your computer and use it in GitHub Desktop.
buffer-weirdness.js
const myString = "hello \u2639 more here";
console.log(myString);
var bufferA = new Buffer(myString, 'binary');
var bufferB = new Buffer(myString, 'utf8');
var bufferC = new Buffer(myString);
console.log(`bufferA length: ${bufferA.length} : "${bufferA}"`);
console.log(`bufferB length: ${bufferB.length} : "${bufferB}"`);
console.log(`bufferC length: ${bufferC.length} : "${bufferC}"`);
console.log('bufferA json', JSON.stringify(bufferA));
console.log('bufferB json', JSON.stringify(bufferB));
console.log('bufferC json', JSON.stringify(bufferC));
# LOL, gist doesnt like utf8 chars :D
hello ☹ more here
bufferA length: 17 : "hello 9 more here"
bufferB length: 19 : "hello ☹ more here"
bufferC length: 19 : "hello ☹ more here"
bufferA json {"type":"Buffer","data":[104,101,108,108,111,32,57,32,109,111,114,101,32,104,101,114,101]}
bufferB json {"type":"Buffer","data":[104,101,108,108,111,32,226,152,185,32,109,111,114,101,32,104,101,114,101]}
bufferC json {"type":"Buffer","data":[104,101,108,108,111,32,226,152,185,32,109,111,114,101,32,104,101,114,101]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment