Skip to content

Instantly share code, notes, and snippets.

@alashow
Last active October 1, 2016 02:57
Show Gist options
  • Save alashow/07d9ef9c02ee697ab47d to your computer and use it in GitHub Desktop.
Save alashow/07d9ef9c02ee697ab47d to your computer and use it in GitHub Desktop.
Convert long integers to "pretty" strings. Could be used for ids in pretty urls
package tm.alashow.playground;
public class PrettyIntegers {
public static void main( String args[] ) {
Character[] map = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K',
'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't',
'u', 'v', 'x', 'y', 'z', '1', '2', '3'};
int num = 1234567890;
String encoded = encode(num, map);
int decoded = decode(encoded, map);
println(num + " => " + encoded);
println(encoded + " => " + decoded);
}
public static String encode( int input, Character[] map ) {
int length = map.length;
String encoded = "";
if (input == 0) {
return String.valueOf(map[0]);
}
while (input > 0) {
int val = input % length;
input = input / length;
encoded += map[val];
}
return encoded;
}
public static int decode( String encoded, Character[] map ) {
int length = map.length;
int decoded = 0;
for(int i = encoded.length() - 1; i >= 0; i--) {
char ch = encoded.charAt(i);
int val = indexOf(ch, map);
decoded = (decoded * length) + val;
}
return decoded;
}
public static <T> int indexOf( T needle, T[] haystack ) {
for(int i = 0; i < haystack.length; i++) {
if (haystack[i] != null && haystack[i].equals(needle)
|| needle == null && haystack[i] == null) return i;
}
return - 1;
}
public static void println( Object object ) {
System.out.println(object);
}
}
function encode(input, map) {
length = map.length;
var encoded = "";
if (input == 0)
return map[0];
while(input > 0) {
val = parseInt(input % length);
input = parseInt(input / length);
encoded += map[val];
}
return encoded;
}
function decode(encoded, map) {
length = map.length;
decoded = 0;
for(i = encoded.length - 1; i >= 0; i--) {
ch = encoded[i];
val = map.indexOf(ch);
decoded = (decoded * length) + val;
}
return decoded;
}
map = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K',
'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't',
'u', 'v', 'x', 'y', 'z', '1', '2', '3'];
encode(1234567890, map) // "VqPeuE"
decode("VqPeuE", map) // "1234567890"
<?php
function encode($input, $map = array()) {
$length = count($map);
$encoded = "";
if ($input == 0)
return $map[0];
while($input > 0) {
$val = intval($input % $length);
$input = intval($input / $length);
$encoded .= $map[$val];
}
return $encoded;
}
function decode($encoded, $map) {
$length = count($map);
$decoded = 0;
for($i = strlen($encoded) - 1; $i >= 0; $i--) {
$ch = $encoded[$i];
$val = array_search($ch, $map);
$decoded = ($decoded * $length) + $val;
}
return $decoded;
}
$map = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K',
'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't',
'u', 'v', 'x', 'y', 'z', '1', '2', '3'];
$encoded = encode(1234567890, $map);
$decoded = decode($encoded, $map);
echo "Encoded: " . $encoded . "<br>Decoded: " . $decoded; //result Encoded: VqPeuE, Decoded: 1234567890
?>
chars = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K',
'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't',
'u', 'v', 'x', 'y', 'z', '1', '2', '3'];
def encode(input):
length = len(chars);
encoded = "";
if input < 0:
input *= -1
encoded += "-";
if input == 0:
return chars[0];
while input > 0:
val = int(input % length);
input = int(input / length);
encoded += chars[val];
return encoded;
def decode(encoded):
length = len(chars);
decoded = 0;
isNegative = 1;
# if starts with minus, cut it and make result negative
if encoded.startswith("-"):
encoded = encoded[1:];
isNegative = -1;
for char in reversed(encoded):
val = chars.index(char);
decoded = (decoded * length) + val;
return decoded * isNegative;
print encode(101310211, chars);
print decode("WYDEW", chars);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment