Skip to content

Instantly share code, notes, and snippets.

@JLarky
Last active March 18, 2024 04:44
Show Gist options
  • Save JLarky/5fb7cc00112bdb6c79759298a1becb6b to your computer and use it in GitHub Desktop.
Save JLarky/5fb7cc00112bdb6c79759298a1becb6b to your computer and use it in GitHub Desktop.
Turn string to number

Get this script

Download the file

curl https://gist.githubusercontent.com/JLarky/5fb7cc00112bdb6c79759298a1becb6b/raw/f84118472bbb6ced9a97e30a49ceddc8bd32357c/str2num.js -o ./str2num.js

Make it executable

chmod +x ./str2num.js

Run this script

./str2num.js hello

or

bash str2num.js hello
#!/bin/sh
/*/.this-doesnt-exist 2>/dev/null
## Please do not edit this part of the script, this is a loader created by "npx bun-self"
if ! [ -x "$(command -v bun)" ]; then
## it's possible that bun is installed but not in the PATH, let's check if BUN_INSTALL is set
if [ -z "$BUN_INSTALL" ]; then
export BUN_INSTALL="$HOME/.bun"
export PATH="$BUN_INSTALL/bin:$PATH"
fi
if ! [ -x "$(command -v bun)" ]; then
echo "Installing bun.sh"
[ -z "$CI" ] && sleep 2
curl -fsSL https://bun.sh/install | bash
echo "Now let's run the script"
echo ""
fi >&2
fi
bun "$0" "$@"
exit 0
#*/
// Script starts here
// https://stackoverflow.com/a/78057133/74167
const cyrb53a = function(str, seed = 0) {
let h1 = 0xdeadbeef ^ seed, h2 = 0x41c6ce57 ^ seed;
for(let i = 0, ch; i < str.length; i++) {
ch = str.charCodeAt(i);
h1 = Math.imul(h1 ^ ch, 0x85ebca77);
h2 = Math.imul(h2 ^ ch, 0xc2b2ae3d);
}
h1 ^= Math.imul(h1 ^ (h2 >>> 15), 0x735a2d97);
h2 ^= Math.imul(h2 ^ (h1 >>> 15), 0xcaf649a9);
h1 ^= h2 >>> 16; h2 ^= h1 >>> 16;
return 2097152 * (h2 >>> 0) + (h1 >>> 11);
};
console.log(cyrb53a(process.argv[2] || ''));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment