base32
base32
- Subject: base32
- From: Sandro Noel <email@hidden>
- Date: Wed, 23 Sep 2009 16:14:35 -0400
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('b', 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 (email@hidden)
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:
This email sent to email@hidden