Gotcha!
Jun 25, 2019 20:26 · 118 words · 1 minute read
Collection of programming gotchas I have personally encountered. And made me confused.
Shell
collapse
echo
vsecho -n
. Use the latter if you want to check for hash.1 2
echo "hello world" | shasum -a 256 # trailing newline is included in hash echo -n "hello world" | shasum -a 256 # use -n to exclude trailing newline
Show repl.it
Loading embedded repl. Click here if it takes too long.
JS
collapse
Watch out for control characters’ padding when encoding to hex.
1 2 3 4
const controlChar = '\n' const hex = Buffer.from(controlChar).toString('hex') hex == controlChar.charCodeAt(0).toString(16) // false hex == controlChar.charCodeAt(0).toString(16).padStart(2, '0') // true
Show repl.it
Loading embedded repl. Click here if it takes too long.