Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

DT.DTDG

macrumors newbie
Original poster
Sep 16, 2013
2
0
I'm moving over from PHP and getting stuck into Objective-C.
One thing I am trying to rewrite is a custom BASE64 function.

How would this be written in Obj-C?

PHP:
public static function decode($string, $key=hYc87.@.l_D3d-)
{
$result = '';
$string = str_replace(' ', '+', $string);
$string = base64_decode($string);
$string = str_replace($key, '', $string);
for($i=0; $i<strlen($string); $i++) {
$char = substr($string, $i, 1);
$keychar = substr($key, ($i % strlen($key))-1, 1);
$char = chr(ord($char)-ord($keychar));
$result.=$char;
}
return $result;
}
 
I'm moving over from PHP and getting stuck into Objective-C.
One thing I am trying to rewrite is a custom BASE64 function.

How would this be written in Obj-C?

PHP:
public static function decode($string, $key=hYc87.@.l_D3d-)
{
$result = '';
$string = str_replace(' ', '+', $string);
$string = base64_decode($string);
$string = str_replace($key, '', $string);
for($i=0; $i<strlen($string); $i++) {
$char = substr($string, $i, 1);
$keychar = substr($key, ($i % strlen($key))-1, 1);
$char = chr(ord($char)-ord($keychar));
$result.=$char;
}
return $result;
}


I can't find any Base64 code that I'm free to share due to copyright/licensing issues.

Erica Sadun's excellent "The Advanced iOS 6 Developer's cookbook" includes code in chapter 12, sample 0, that shows how to do the base 64 encoding/decoding, but I'm not sure if I'm free to post that code here for non-owners of the book.

The code is a category of NSData that includes a class method to create NSData from a Base64 string and an instance method that will return the Base64 string for an NSData object.

I suggest buying the book. It's chock-full of great resources like this. The
"Core iOS 6 Developer's cookbook" title is excellent too. I highly recommend both to all working developers who use iOS.

The books might not be the best choice for newbies, but for intermediate-to-advanced developers, they are invaluable. Within a few days of buying the previous iOS 5 title I found the solution to a problem that was dogging me for days, and the book paid for itself.
 
Thanks Duncan C and PhoneyDeveloper!

I've sorted it out now :) it was basically the unicode vs byte1 representation from PHP to Objective-C..

Thank you again!
 
Thanks Duncan C and PhoneyDeveloper!

I've sorted it out now :) it was basically the unicode vs byte1 representation from PHP to Objective-C..

Thank you again!

There are C libraries that does this as well, if you look at the examples at SO for example you'll see that many comments points out subtle bugs and memory leaks. A base64 encoder operates on primitive types, which makes it doable in C, an unsigned char is all you need.
 
Thanks Duncan C and PhoneyDeveloper!

I've sorted it out now :) it was basically the unicode vs byte1 representation from PHP to Objective-C..

Thank you again!

Hi DT.DTDG,

it would be great, if you would share your working code here and give me the chance to learn from you.

I´m also stuck in the same problem and need exactly the same.

Many many thanks in advance.

Regards
fxworker
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.