What I need is to convert between base 16 that is hex into an arbitrary another base, like 36, that is 0-9 and small letters a-z or even a base 62 that is [0-9a-zA-Z].

The problem is that built in php functions return incorrect values from large numbers like this hash:

3971ebf11a18bce250a282e6ff78739c9a0f7bcb9bc3552fe9cd8191e4a4da573bb38983a47b2a86a63bfc4e41d3361d5d6ae1a1480bf3f27f730cc75fbc84de

I need to compress it to less length using all possible numbers and letters.
I cannot use gmp plugin because of my server limitation.
I do not care about speed of eventual solution, it won't be run very often.

    a year later

    i had the same problem - came up with an interesting solution for anyone who stumbles on this page (i assume the OP found a solution by now)

    so the idea is not to pick an arbitrary base, you will waste bits

    echo bit_up("268ad52b8573137664ea27e557759d1f",4,5);
    (function source below)

    turns
    268ad52b8573137664ea27e557759d1f (32 chars)
    into
    hbain1bj2dr69qh7slbnb78v (26 chars)
    or
    fCyJkHxncjtCjG9-lntpQv (22 chars)

    so you're basically saying each char in the first sequence is 4 bits (base 16) and you want to convert it to 5 bits per char (base 32) or 6 bits (base 64).

    i haven't tried to reverse the function which might not work without some additional padding, and a special reverse binary function

    in theory, if you had 256 characters to use, you would cut the string in half (4 bits + 4 bits = 8 bits = 256 chars)

    here's the function source

    function bit_up($code,$start_bits,$end_bits){
    	// convert to binary
    	$from_base = pow (2 ,$start_bits);
    	$length = strlen($code);
    	$binary = '';
    	$i = 0;
    
    while($i < $length){
    //		echo substr($code,$i,1)."*";
    		$binary .= str_pad(base_convert(substr($code,$i,1), $from_base, 2),$start_bits,"0",STR_PAD_LEFT);
    		$i ++;
    	}
    	echo "\n".$binary."\n";
    
    // convert to new base
    $char_base = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_";
    $i = strlen($binary)-$end_bits;
    $output = "";
    while($i > 0-$end_bits){
    	$char = base_convert(substr($binary,$i,$end_bits),2,10);
    	$output = $char_base[$char] . $output;
    	$i -= $end_bits;
    }
    return $output;
    }
      Write a Reply...