Search code examples
phpencodingdecoding

Convert hex to ascii characters


Is it possible to represent a sequence of hex characters (0-9A-F) with a sequence of 0-9a-zA-Z characters, so the the result sequence is smaller and can be decoded?

For example:

$hex = '5d41402abc4b2a76b9719d911017c592';
echo $string = encode($hex); // someASCIIletters123
echo decode(string) == $hex; //true

Solution

  • You can trivially adapt the solution I presented here using the function base_convert_arbitrary.

    Edit: I had not read carefully enough :) Base 16 to base 62 is still very doable, as above.

    See it in action.