Created
August 3, 2017 10:21
-
-
Save philpoore/4c8535814fc4cfb4a2bd7e2f9740f2de to your computer and use it in GitHub Desktop.
buffer-weirdness.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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