Last active
August 29, 2024 14:15
-
-
Save kevinyan815/f71b2f5ca3541631abd2e50f3929739b to your computer and use it in GitHub Desktop.
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 toBaseN(num, base) { | |
if (num === 0) { | |
return '0'; | |
} | |
var digits = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
var len = Math.min(digits.length, base); | |
var result = ''; | |
while (num > 0) { | |
result = digits[num % len] + result; | |
num = parseInt(num / len, 10); | |
} | |
return result; | |
} | |
function fromBaseN(str, base) { | |
if (str === null || str.length === 0) { | |
return 0; | |
} | |
var digits = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
var len = Math.min(digits.length, base); | |
var result = 0; | |
for (var i=0 ; i<str.length ; i++) { | |
var p = digits.indexOf(str[i]); | |
if (p < 0 || p >= base) { | |
return NaN; | |
} | |
result += p * Math.pow(digits.length, str.length - i - 1); | |
} | |
return result; | |
} | |
function toBase62(n) { | |
if (n === 0) { | |
return '0'; | |
} | |
var digits = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
var result = ''; | |
while (n > 0) { | |
result = digits[n % digits.length] + result; | |
n = parseInt(n / digits.length, 10); | |
} | |
return result; | |
} | |
function fromBase62(s) { | |
var digits = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
var result = 0; | |
for (var i=0 ; i<s.length ; i++) { | |
var p = digits.indexOf(s[i]); | |
if (p < 0) { | |
return NaN; | |
} | |
result += p * Math.pow(digits.length, s.length - i - 1); | |
} | |
return result; | |
} |
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
if (!function_exists('base62_encode')) { | |
/** | |
* Convert a 10 base numeric string to a 62 base string | |
* | |
* @param int $value | |
* @return string | |
*/ | |
function base62_encode($value) | |
{ | |
return to_base($value, 62); | |
} | |
} | |
if (!function_exists('base62_decode')) { | |
/** | |
* Convert a string from base 62 to base 10 numeric string | |
* | |
* @param string $value | |
* @return int | |
*/ | |
function base62_decode($value) | |
{ | |
return to_base10($value, 62); | |
} | |
} | |
if (!function_exists('to_base')) { | |
/** | |
* Convert a numeric string from base 10 to another base. | |
* | |
* @param $value decimal string | |
* @param int $b base , max is 62 | |
* @return string | |
*/ | |
function to_base($value, $b = 62) | |
{ | |
$base = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
$r = $value % $b; | |
$result = $base[$r]; | |
$q = floor($value / $b); | |
while ($q) { | |
$r = $q % $b; | |
$q = floor($q / $b); | |
$result = $base[$r] . $result; | |
} | |
return $result; | |
} | |
} | |
if (!function_exists('to_base10')) { | |
/** | |
* Convert a string from a given base to base 10. | |
* | |
* @param string $value string from given base | |
* @param int $b base, max is 62 | |
* @return string | |
*/ | |
function to_base10($value, $b = 62) | |
{ | |
$base = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
$limit = strlen($value); | |
$result = strpos($base, $value[0]); | |
for ($i = 1; $i < $limit; $i++) { | |
$result = $b * $result + strpos($base, $value[$i]); | |
} | |
return $result; | |
} | |
} |
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
#!/usr/bin/python | |
# -*- coding=UTF-8 -*- | |
from __future__ import print_function, division | |
BASE62 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
def encode(num, alphabet=BASE62): | |
"""Encode a positive number in Base X | |
Arguments: | |
- `num`: The number to encode | |
- `alphabet`: The alphabet to use for encoding | |
""" | |
if num == 0: | |
return alphabet[0] | |
arr = [] | |
base = len(alphabet) | |
while num: | |
num, rem = divmod(num, base) | |
arr.append(alphabet[rem]) | |
arr.reverse() | |
return ''.join(arr) | |
def decode(string, alphabet=BASE62): | |
"""Decode a Base X encoded string into the number | |
Arguments: | |
- `string`: The encoded string | |
- `alphabet`: The alphabet to use for encoding | |
""" | |
base = len(alphabet) | |
strlen = len(string) | |
num = 0 | |
idx = 0 | |
for char in string: | |
power = (strlen - (idx + 1)) | |
num += alphabet.index(char) * (base ** power) | |
idx += 1 | |
return num | |
if __name__ == '__main__': | |
print(encode(123004569870)) | |
print(decode('2agqDFK')) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment