X
27May

Base64 encoding in Objective c

When we have some binary data that need to send across network, we generally don't do it by just streaming the bits and bytes over the wire in a raw format. 

Why ?

Because some media are made for streaming text. You never know some protocols may interpret your binary data as control characters (like a modem), or your binary data could be screwed up because the underlying protocol might think that you've entered a special character combination (like how FTP translates line endings).So to get around this, people encode the binary data into characters. Base64 is one of these types of encodings.

Why 64?

Because you can generally rely on the same 64 characters being present in many character sets, and you can be reasonably confident that your data's going to end up on the other side of the wire uncorrupted.

Here is process to convert nsstring data into Base64 format in objective c:-

You can get Base64 library from here:-https://github.com/nicklockwood/Base64/

Add Base64.h and Base64.m in project folder

or create two classes Base64.h and Base64.m and put the following code into respective classes:-

//put this code into your Base64.h

#import

@interface Base64 : NSObject {

}

+ (void) initialize;

+ (NSString*) encode:(const uint8_t*) input length:(NSInteger) length;

+ (NSString*) encode:(NSData*) rawBytes;

+ (NSData*) decode:(const char*) string length:(NSInteger) inputLength;

+ (NSData*) decode:(NSString*) string;

@end

 

 

//put this code into your Base64.m

#import "Base64.h"

@implementation Base64

#define ArrayLength(x) (sizeof(x)/sizeof(*(x)))

static char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

static char decodingTable[128];

+ (void) initialize {

if (self == [Base64 class]) {

memset(decodingTable, 0, ArrayLength(decodingTable));

for (NSInteger i = 0; i < ArrayLength(encodingTable); i++) {

decodingTable[encodingTable[i]] = i;

}

}

}

 

+ (NSString*) encode:(const uint8_t*) input length:(NSInteger) length {

    NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];

    uint8_t* output = (uint8_t*)data.mutableBytes;

   for (NSInteger i = 0; i < length; i += 3) {

        NSInteger value = 0;

        for (NSInteger j = i; j < (i + 3); j++) {

            value <<= 8;

 

            if (j < length) {

                value |= (0xFF & input[j]);

            }

        }

 

        NSInteger index = (i / 3) * 4;

        output[index + 0] =                    encodingTable[(value >> 18) & 0x3F];

        output[index + 1] =                    encodingTable[(value >> 12) & 0x3F];

        output[index + 2] = (i + 1) < length ? encodingTable[(value >> 6)  & 0x3F] : '=';

        output[index + 3] = (i + 2) < length ? encodingTable[(value >> 0)  & 0x3F] : '=';

    }

   return [[NSString alloc] initWithData:data

                                  encoding:NSASCIIStringEncoding];

}

 

+ (NSString*) encode:(NSData*) rawBytes {

    return [self encode:(const uint8_t*) rawBytes.bytes length:rawBytes.length];

}

 

+ (NSData*) decode:(const char*) string length:(NSInteger) inputLength {

if ((string == NULL) || (inputLength % 4 != 0)) {

return nil;

}

 

while (inputLength > 0 && string[inputLength - 1] == '=') {

inputLength--;

}

 

NSInteger outputLength = inputLength * 3 / 4;

NSMutableData* data = [NSMutableData dataWithLength:outputLength];

uint8_t* output = data.mutableBytes;

 

NSInteger inputPoint = 0;

NSInteger outputPoint = 0;

while (inputPoint < inputLength) {

char i0 = string[inputPoint++];

char i1 = string[inputPoint++];

char i2 = inputPoint < inputLength ? string[inputPoint++] : 'A'; /* 'A' will decode to \0 */

char i3 = inputPoint < inputLength ? string[inputPoint++] : 'A';

 

output[outputPoint++] = (decodingTable[i0] << 2) | (decodingTable[i1] >> 4);

if (outputPoint < outputLength) {

output[outputPoint++] = ((decodingTable[i1] & 0xf) << 4) | (decodingTable[i2] >> 2);

}

if (outputPoint < outputLength) {

output[outputPoint++] = ((decodingTable[i2] & 0x3) << 6) | decodingTable[i3];

}

}

 

return data;

}

 

+ (NSData*) decode:(NSString*) string {

return [self decode:[string cStringUsingEncoding:NSASCIIStringEncoding] length:string.length];

}

 

@end

 
//   Declare  this in your .h class file:-

  #import "Base64.h"

//In .m file you can convert the nsstring data into Base64 where you need by follow this :-

 

    NSString *actualStr = [NSString stringWithFormat:@"%@",@"given string which need to convert into base64"];

    NSData   *convertedData = [actualStr dataUsingEncoding:NSUTF8StringEncoding];

    NSString *convertedBase64Val; = [NSString stringWithFormat:@"%@",[Base64 encode:convertedData]];

 

Now you got your Base64 data here as "convertedBase64Val.

 

 

Related

Access Control in Swift 3

End of this blog you will be able to understand the Access control specifiers open, public,internal,...

Read More >

How to handle the exceptions using recovery scenario manager in QTP?

VB script Error HandlingDurning run time, if QTP encounters any erorr then it will display the error...

Read More >

Creating Apps Using Phonegap Framework in IOS

Creating apps for different operating system is not a reliability so for reducing developr work for ...

Read More >

Memory Management in iOS

Memory Management iOS In this blog we will walk through very important thing which all iOS developer...

Read More >

Implementing iOS Setting Bundle

 iOS Setting Bundle:-Actually sometimes we need to interact our app i.e giving some input value...

Read More >

Wireless Ad-Hoc distribution for iOS apps

Creating Ad-Hoc is an important part of iOS app development because it helps us in testing the app d...

Read More >

iOS qrcode generator

Have you heard of QR Codes yet? Here is a quick introduction:- QR is short for Quick Response . It i...

Read More >

How to Create a Provisioning profile for iOS

To Run an app on iOS device,you must have a Provisioning Profile installed on your device. ...

Read More >

How to check network reachability in iOS

What is network reachability? Now, you are all set to program your app on the ios device? Here is on...

Read More >

Share

Try DevOpSmartBoard Ultimate complete Azure DevOps End-to end reporting tool

Sign Up

  • Recent
  • Popular
  • Tag
Monthly Archive
Subscribe
Name

Text/HTML
Text/HTML
Contact Us
  • *
  • *