This is probably stupid but i need to ask, sorry.

I've been looking all around the internet for a simple Base 32 encode/ decode cocoa function but all i can find are somehow not compatible with the PHP implementation on my web site.

see, if i encode and then decode on the website everything validates perfectly. ( same value in as value out ) if i use the same encoded string and try to decode with my local computer it does not validate.

so i'm trying to find a simple cocoa base32 encode/decode function that would be as simple as this php one,.
or a good specification that i can build from.. Any pointer would be ok,

thank you in advance.

Sandro


<?php
function base32_encode($input) {
        // Get a binary representation of $input
        $binary = unpack('C*', $input);
        $binary = vsprintf(str_repeat('%08b', count($binary)), $binary);

        $binaryLength = strlen($binary);
        $base32_characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";      
        $currentPosition = 0;
        $output = '';

        while($currentPosition < $binaryLength) {
                $bits = substr($binary, $currentPosition, 5);

                if(strlen($bits) < 5)
                        $bits = str_pad($bits, 5, "0");

                // Convert the 5 bits into a decimal number
                // and append the matching character to $output
                $output .= $base32_characters[bindec($bits)];
                $currentPosition += 5;
        }

        // Pad the output length to a multiple of 8 with '=' characters
        $desiredOutputLength = strlen($output);
        if($desiredOutputLength % 8 != 0) {
                $desiredOutputLength += (8 - ($desiredOutputLength % 8));
                $output = str_pad($output, $desiredOutputLength, "=");
        }

        return $output;
}
?>

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to