Base64 Encode in Objective-C

I am currently playing about with objective-c and came across the need to base 64 encode some text. The following is what I came up with however it can obviously do with some improving (note to self must plan code properly before jumping in).

base64.h
// Created by Peter McDonald on 17/07/2011.
// Copyright 2011 n/a. All rights reserved.
//

#import

@interface base64 : NSObject
{
NSArray *ascii;
}

-(NSString)encode : (NSString)text;
-(NSString)intToBinaryString:(int)intValue;
-(NSString
)binaryStringToNsstring:(NSString*)binaryStringValue;

@end
base64.m
// Created by Peter McDonald on 17/07/2011.
// Copyright 2011 n/a. All rights reserved.
//

#import “base64.h”

@implementation base64

  • (id)init
    {
    self = [super init];
    if (self) {
    ascii = [[NSArray alloc] initWithObjects:
    @”A” ,@”B” ,@”C” ,@”D” ,@”E” ,@”F” ,@”G” ,@”H” ,@”I” ,@”J” ,@”K” ,@”L” ,@”M” ,
    @”N” ,@”O” ,@”P” ,@”Q” ,@”R” ,@”S” ,@”T” ,@”U” ,@”V” ,@”W” ,@”X” ,@”Y” ,@”Z” ,
    @”a” ,@”b” ,@”c” ,@”d” ,@”e” ,@”f” ,@”g” ,@”h” ,@”i” ,@”j” ,@”k” ,@”l” ,@”m” ,
    @”n” ,@”o” ,@”p” ,@”q” ,@”r” ,@”s” ,@”t” ,@”u” ,@”v” ,@”w” ,@”x” ,@”y” ,@”z” ,
    @”0″ ,@”1″ ,@”2″ ,@”3″ ,@”4″ ,@”5″ ,@”6″ ,@”7″ ,@”8″ ,@”9″ ,@”+” ,@”/” ,
    nil];
    }
    return self;
    }

-(NSString)encode:(NSString *)text
{
int stringLength = [text length];
NSString *newString = [[NSString alloc] init];
NSString *binaryString = [[NSString alloc] init];
int currentCharAsInt;
for (int i = 0; i < stringLength; i++) { currentCharAsInt = [text characterAtIndex:i]; binaryString = [binaryString stringByAppendingString:[self intToBinaryString:currentCharAsInt]]; } newString = [self binaryStringToNsstring:binaryString]; return newString; } -(NSString
)decode:(NSString )text { return @””; } -(NSString)intToBinaryString:(int)intValue { int current= 128; NSString *returnString = [[NSString alloc] init]; while (current >= 1)
{
if(intValue >= current)
{
intValue = intValue – current;
returnString = [returnString stringByAppendingString:@”1″];
}
else
{
returnString = [returnString stringByAppendingString:@”0″];
}
if (current > 1)
{
current = current / 2;
}
else
{
current = current – 1;
}

}
return returnString;
}

-(NSString)binaryStringToNsstring:(NSString)binaryStringValue
{
NSString *returnString = [[NSString alloc] init];
int binaryStringValueLength = [binaryStringValue length];
if (binaryStringValue > 0)
{
NSString *workingString = [NSString alloc];
int currentIndex = 0;
NSRange range = {currentIndex, 6};
workingString = [workingString initWithFormat:[binaryStringValue substringWithRange:range]];
while ([workingString length] > 0)
{
int currentBinaryValue = 1;
NSUInteger currentValue = 0;
for (int i = 0; i < [workingString length]; i++) { NSRange charRange = {[workingString length] – i – 1, 1}; NSString *currentChar = [[NSString alloc] initWithFormat:[workingString substringWithRange:charRange]]; if ([currentChar isEqualToString:@”1″]) { currentValue = currentValue + currentBinaryValue; } currentBinaryValue = currentBinaryValue * 2; currentIndex++; } if ((currentIndex + 1) >= binaryStringValueLength)
{
workingString = @””;
}
else if ((currentIndex + 6) > binaryStringValueLength)
{
NSRange range = {currentIndex, binaryStringValueLength – currentIndex};
workingString = [[NSString alloc] initWithFormat:[binaryStringValue substringWithRange:range]];
if ([workingString length] == 2)
{
workingString = [workingString stringByAppendingString:@”0000″];
}
else
{
workingString = [workingString stringByAppendingString:@”00″];
}
}
else
{
NSRange range = {currentIndex, 6};
workingString = [[NSString alloc] initWithFormat:[binaryStringValue substringWithRange:range]];
}
returnString = [returnString stringByAppendingString:[ascii objectAtIndex:currentValue]];
currentValue = 0;
currentBinaryValue = 1;
}
while ([returnString length] % 4 != 0)
{
returnString = [returnString stringByAppendingString:@”=”];
}
}
return returnString;
}

@end
The code currently does not add a CRLF character after every 78 characters at the moment however the program this has been designed for does not require it (although I will add this once I get around to rewriting the methods),

Any suggestions let me know.

One thought on “Base64 Encode in Objective-C”

  1. Hello, i think that i saw you visited my web site thus i came to “return the favor”.I am trying to discover points to enhance my internet internet site!I suppose its ok to use some of your suggestions!!

Tell us your thoughts

This site uses Akismet to reduce spam. Learn how your comment data is processed.