Gotcha!

Jun 25, 2019 20:26 · 118 words · 1 minute read programming

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.

    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

  1. 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.

 View on Github Last updated Jun 29, 2019 (4:31am) 
  Keep It Sane (Jun 16)