How to convert a hex or decimal to base64

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
WaldoMonster
Forum Contributor
Posts: 225
Joined: Mon Apr 19, 2004 6:19 pm
Contact:

How to convert a hex or decimal to base64

Post by WaldoMonster »

I know the base64_encode command but it is for binary data.
And base_convert is limited to base 36.
Any help will be appreciated.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Everything is always for binary. Hex and decimal characters are still binary. You can use base64_encode() for anything.
User avatar
WaldoMonster
Forum Contributor
Posts: 225
Joined: Mon Apr 19, 2004 6:19 pm
Contact:

Post by WaldoMonster »

I mend a number transformation.
The same way you can convert a decimal to hex with $hex = baseconvert($dec, 10, 16);

11 decimal = a base64
62 decimal = Z base64
hedge
Forum Contributor
Posts: 234
Joined: Fri Aug 30, 2002 10:19 am
Location: Calgary, AB, Canada

Post by hedge »

I don't think base64_encode is what you want. Base 64 encoding is used for converting binary data to string such as putting attachments in email, It doesn't do number system conversions.

You'd have to write your own function to do that conversion just curious but what characters are used for base64? upper and lowercase maybe?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

a-zA-Z0-9 and a few symbols..

to find out the exact ones:

Code: Select all

<?php

$stream = '';
for($x = 0; $x < 64; $x++) $stream .= chr($x);

echo base64_encode($stream);

?>
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Its:

26 - lowercase letters
26 - uppercase letters
10 - numbers
2 - + and /

64 values - hense the name.
User avatar
WaldoMonster
Forum Contributor
Posts: 225
Joined: Mon Apr 19, 2004 6:19 pm
Contact:

Post by WaldoMonster »

This is what I use now to convert hex to base64:

Code: Select all

$base64 = base64_encode(pack('H*', $hex));
Post Reply