[ create a new paste ] login | about

Link: http://codepad.org/hYEBXNCc    [ raw code | output | fork ]

PHP, pasted on Jul 18:
<?php

function crockford_encode( $base10 ) {
    return strtr( base_convert( $base10, 10, 32 ),
                  "abcdefghijklmnopqrstuv",
                  "ABCDEFGHJKMNPQRSTVWXYZ" );
}

function crockford_decode( $base32 ) {
    $base32 = strtr( strtoupper( $base32 ), 
                     "ABCDEFGHJKMNPQRSTVWXYZILO",
                     "abcdefghijklmnopqrstuv110" );
    return base_convert( $base32, 32, 10 );
}

echo crockford_encode( '519571' ), "\n";
echo crockford_decode( 'FVCK' ), "\n";

# Self-test:
$failed = 0;
for ( $i = 0; $i < 1024; $i++ ) {
    $enc = crockford_encode( $i );
    $dec = crockford_decode( $enc );
    if ( $dec != $i ) {
        $failed++;
        echo "Self-test failed: $i -> $enc -> $dec\n";
    }
}
if ( ! $failed ) echo "Self-test passed OK.\n";


Output:
1
2
3
FVCK
519571
Self-test passed OK.


Create a new paste based on this one


Comments: