7

Here is my sample code in objective C

 -(NSString *)getImageString : (unsigned char *) charValue : (unsigned long) sizeOfBytes {                   

    uint8_t commandbyte[]={ };          

    uint8_t _allBytes[(sizeOfBytes + sizeof(commandbyte))];
    memcpy(_allBytes, charValue, sizeOfBytes);

    NSMutableData *ImageData = [[NSMutableData alloc]init];
    [ImageData appendBytes:_allBytes length:sizeof(_allBytes)];

    NSString *base64String=[self base64forData:ImageData];

    return base64String;                     
}                  

- (NSString*)base64forData:(NSData*)theData {           

    const uint8_t* input = (const uint8_t*)[theData bytes];
    NSInteger length = [theData length];

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

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

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

        for (j = i; j < (i + 3); j++) {
            value <<= 8;

            if (j < length) {
                value |= (0xFF & input[j]);
            }
        }

        NSInteger theIndex = (i / 3) * 4;
        output[theIndex + 0] = table[(value >> 18) & 0x3F];
        output[theIndex + 1] = table[(value >> 12) & 0x3F];
        output[theIndex + 2] = (i + 1) < length ? table[(value >> 6)  & 0x3F] : '=';
        output[theIndex + 3] = (i + 2) < length ? table[(value >> 0)  & 0x3F] : '=';
    }

    return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
}

In this example i used to get the bytes using the sizeOfBytes and then appendBytes to NSMutableData.

Below method is used to convert data to base64:

- (NSString*)base64forData:(NSData*)theData

It is very simple in objective C, but when i tried in swift there other concepts for pointers like UnsafeMutablePointer, UnsafePointer etc.

How to convert to swift 3.0 ??

Can you Guys please suggest me the usage of pointers in swift

Bista
  • 7,869
  • 3
  • 27
  • 55
naresh d
  • 183
  • 1
  • 3
  • 8
  • 2
    Use Data, read http://stackoverflow.com/questions/39515173/how-to-use-unsafemutablepointer-in-swift-3 – Ludovic Jan 06 '17 at 08:58
  • What's your actual purpose? You want to practice how to work with Swift `Data` and pointers, even if you know you can use the `base64EncodedString` method? – OOPer Jan 06 '17 at 11:38

2 Answers2

15

You can convert bytes array to base64String by using following way

let base64String = data!.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
Usman Javed
  • 2,437
  • 1
  • 17
  • 26
-1

Swift 5 Convert bytes array to base64String using following ways

Example 1

let plainText = "Hello world!"
print("Plain Text:", plainText)
print("Base64String:", Array(plainText.utf8).toBase64()!)

Output Example 1

Plain Text: Hello world!
Base64String: SGVsbG8gd29ybGQh

Example 2

let ivBase64str = "AbcnmWikMkW4c7+mHtwtfw=="
let iv = [UInt8](base64: ivBase64str)
print("BytesArray:", iv)
print("Base64String:", iv.toBase64()!)

Output Example 2

BytesArray: [1, 183, 39, 153, 104, 164, 50, 69, 184, 115, 191, 166, 30, 220, 45, 127]
Base64String: AbcnmWikMkW4c7+mHtwtfw==
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
Kiran Patil
  • 402
  • 5
  • 13