wzulfikar.com

Gotcha!

Cover Image for Gotcha!

Collection of programming gotchas I have personally encountered. And made me confused.

Shell

collapse
  1. echo vs echo -n. Use the latter if you want to check for hash.

    {{< highlight bash "linenos=table" >}}
    

    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{{< / highlight >}}

    {{< show-repl-it url="https://repl.it/@wzulfikar/shell-gotcha-echo?lite=true" >}}
    

JS

collapse
  1. Watch out for control characters' padding when encoding to hex.

    {{< highlight php "linenos=table" >}}
    

    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{{< / highlight >}}

    {{< show-repl-it url="https://repl.it/@wzulfikar/js-gotcha-char-code?lite=true" >}}