Skip to content

Instantly share code, notes, and snippets.

@Nomon
Created June 14, 2011 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nomon/1024410 to your computer and use it in GitHub Desktop.
Save Nomon/1024410 to your computer and use it in GitHub Desktop.
ahex
var id = "4df67d6dcc19519d0f098bd6"
var ahex = id.split('').map(function(a) {
// isNumber
if((a - 0) == a && a.length > 0) {
// g because 0 would collide with hex f. 0->"g",1->"h","a"->"a" etc
return String.fromCharCode("g".charCodeAt(0)+Number(a));
}
return a;
}).join('');
var unhex = ahex.split('').map(function(a) {
// character is encoded number, larger than "f"
if(a.charCodeAt(0) > "f".charCodeAt(0)) {
return String.fromCharCode("0".charCodeAt(0) + a.charCodeAt(0)-"g".charCodeAt(0));
}
// character less than "g" so part of hex
return a;
}).join('');
console.dir(id);
console.dir(ahex);
console.dir(unhex);
<?php
$new_ahex_id = null;
$ahex_id = "4df67d6dcc19519d0f098bd6";
echo $ahex_id."\n";
$new_ahex_id = '';
for ($i=0; $i < strlen($ahex_id); $i++) {
if (is_numeric(substr($ahex_id, $i, 1))) {
$char = chr(ord("g") + (int)substr($ahex_id, $i, 1));
$new_ahex_id .= $char;
} else {
$new_ahex_id .= substr($ahex_id, $i, 1);
}
}
echo $new_ahex_id."\n";
$unhex = '';
for ($i=0; $i <strlen($ahex_id); $i++) {
if (ord(substr($ahex_id, $i, 1) ) > ord("f")) {
$char = chr(ord("0") + ord(substr($ahex_id, $i, 1)) - ord("g")) ;
$unhex .= $char;
} else {
$unhex .= substr($ahex_id, $i, 1);
}
}
echo $unhex."\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment