Last active
March 6, 2022 05:02
-
-
Save fabiolimace/6de4b8cc44e7a488360dd6a0988f70be to your computer and use it in GitHub Desktop.
UUID to any base-n for Python and Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function basen(number, alphabet) { | |
let output = "" | |
let base = alphabet.length; | |
while (number > 0) { | |
output += alphabet.charAt(Number(BigInt(number) % BigInt(base))) | |
number = BigInt(number) / BigInt(base) | |
} | |
return output.split("").reverse().join("") | |
} | |
function encode(uuid, alphabet) { | |
let num = uuid | |
let pad = alphabet.charAt(0) | |
let base = alphabet.length; | |
let length = Math.floor(Math.ceil(128 / (Math.log(base) / Math.log(2)))) | |
let encoded = basen(num, alphabet) | |
return encoded.padStart(length, pad) | |
} | |
function random() { // 32 bit random | |
return Math.floor(Math.random() * (2 ** 32)) | |
} | |
function uuid4() { // FAKE UUID as BigInt | |
return BigInt(random()) << 96n | BigInt(random()) << 64n | BigInt(random()) << 32n | BigInt(random()) | |
} | |
function main() { | |
let uuid = uuid4() | |
// let uuid = 0x09d4579743ba4ae59c92481d29650a19n | |
let b02 = encode(uuid, "01") | |
let b08 = encode(uuid, "01234567") | |
let b10 = encode(uuid, "0123456789") | |
let b16 = encode(uuid, "0123456789abcdef") | |
let b32 = encode(uuid, "abcdefghijklmnopqrstuvwxyz234567") | |
let b36 = encode(uuid, "0123456789abcdefghijklmnopqrstuvwxyz") | |
let b48 = encode(uuid, "ABCDEFGHJKLMNOPQRSTVWXYZabcdefghijkmnopqrstvwxyz") | |
let b52 = encode(uuid, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") | |
let b58 = encode(uuid, "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz") // btc | |
let b62 = encode(uuid, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") | |
let b64 = encode(uuid, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_") // url | |
console.log(b02) | |
console.log(b08) | |
console.log(b10) | |
console.log(b16) | |
console.log(b32) | |
console.log(b36) | |
console.log(b48) | |
console.log(b52) | |
console.log(b58) | |
console.log(b62) | |
console.log(b64) | |
} | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python3 | |
import math | |
import string | |
from uuid import UUID, uuid4 | |
def basen(number, alphabet): | |
output = "" | |
base = len(alphabet) | |
while number: | |
output += alphabet[number % base] | |
number //= base | |
return output[::-1] | |
def encode(uuid, alphabet): | |
num = uuid.int | |
pad = alphabet[0] | |
base = len(alphabet) | |
length = int(math.ceil(128 / (math.log(base) / math.log(2)))) | |
encoded = basen(num, alphabet) | |
return encoded.rjust(length, pad) | |
def main(): | |
uuid = uuid4() | |
# uuid = UUID(int=0x09d4579743ba4ae59c92481d29650a19) | |
b02 = encode(uuid, "01") | |
b08 = encode(uuid, "01234567") | |
b10 = encode(uuid, "0123456789") | |
b16 = encode(uuid, "0123456789abcdef") | |
b32 = encode(uuid, "abcdefghijklmnopqrstuvwxyz234567") | |
b36 = encode(uuid, "0123456789abcdefghijklmnopqrstuvwxyz") | |
b48 = encode(uuid, "ABCDEFGHJKLMNOPQRSTVWXYZabcdefghijkmnopqrstvwxyz") | |
b52 = encode(uuid, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") | |
b58 = encode(uuid, "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz") # btc | |
b62 = encode(uuid, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") | |
b64 = encode(uuid, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_") # url | |
print(b02) | |
print(b08) | |
print(b10) | |
print(b16) | |
print(b32) | |
print(b36) | |
print(b48) | |
print(b52) | |
print(b58) | |
print(b62) | |
print(b64) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: