Created
June 14, 2011 06:09
-
-
Save Nomon/1024410 to your computer and use it in GitHub Desktop.
ahex
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
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); |
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
<?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