Skip to content
Advertisement

how to convert from base64 to hexadecimal in php?

I was thinking to use this:

JavaScript

The output for this example should always be 18 characters! But sometimes this output is less than 18.

24 (base64 characters) multiplied by 6 (bits per base64 character) equals to 144 (bits) divided by 8 (bits per ASCII character) equals to 18 ASCII characters.

The problem is that the output is displayed in plain text; and some characters don’t even have a “text representation” and that data will be lost. The next test will show that there are 41 different ASCII characters with no visible output.

JavaScript

My plan was to decode the base64 string and from the output in ASCII reconvert it to hexadecimal. Now that is not possible because of those 41 characters.

I also tried base_convert but there is no base64 support for it.

Advertisement

Answer

You can do this with bin2hex():

Returns an ASCII string containing the hexadecimal representation of str. The conversion is done byte-wise with the high-nibble first.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement