Skip to content

Instantly share code, notes, and snippets.

@sum-catnip
Last active January 26, 2021 15:46
Show Gist options
  • Save sum-catnip/57ebbebe8bd3a7538909259c678b897a to your computer and use it in GitHub Desktop.
Save sum-catnip/57ebbebe8bd3a7538909259c678b897a to your computer and use it in GitHub Desktop.
javascript v8 double base36 decode/encode in python
# this is specific to the v8 js engine
# firefox actually does something slightly different so their encodings are incompatible
import sys
import math
import string
from functools import reduce
import numpy as np
from numpy import float64 as f64
alpha = string.digits + string.ascii_lowercase
# example input: ruse0iihfcd
# example output: 0.7737566290658499
def b36_double(b36):
return reduce(lambda a, x: f64(alpha.index(x) + a) / 36, reversed(b36), 0)
# sadly python doesnt have an `unfold` so this looks a bit messy
def double_b36(d):
d = f64(d)
delta = f64(0.5) * (np.nextafter(d, 1) - d)
# if d is a smoll boi
delta = max(np.nextafter(f64(0), 1), delta)
res = ''
while d >= delta:
d *= 36
delta *= 36
x = math.floor(d)
d -= x
res += alpha[x]
return res
#print(double_b36(0.123456789))
print(b36_double(sys.argv[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment