function encodeBase58($hex)
{
    if (strlen($hex) % 2 != 0) {
        die("encodeBase58: uneven number of hex characters");
    }
    $orighex = $hex;
    $chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
    $hex = decodeHex($hex);
    $return = "";
    while (bccomp($hex, 0) == 1) {
        $dv = (string) bcdiv($hex, "58", 0);
        $rem = (int) bcmod($hex, "58");
        $hex = $dv;
        $return = $return . $chars[$rem];
    }
    $return = strrev($return);
    //leading zeros
    for ($i = 0; $i < strlen($orighex) && substr($orighex, $i, 2) == "00"; $i += 2) {
        $return = "1" . $return;
    }
    return $return;
}
function encodeBase58($hex)
{
    $base58chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
    if (strlen($hex) % 2 != 0) {
        die("encodeBase58: uneven number of hex characters");
    }
    $orighex = $hex;
    $hex = decodeHex($hex);
    $return = "";
    while (gmp_cmp($hex, 0) > 0) {
        $dv = gmp_strval(gmp_div_q($hex, "58", 0));
        $rem = gmp_strval(gmp_mod($hex, "58"));
        $hex = $dv;
        $return = $return . $base58chars[$rem];
    }
    $return = strrev($return);
    for ($i = 0; $i < strlen($orighex) && substr($orighex, $i, 2) == "00"; $i += 2) {
        $return = "1" . $return;
    }
    return $return;
}