1

I want to base64 encode a NSString. But got the wrong result. Is it the bug of iOS base64 encoding? The int number in characters[] is ASCII code of the character.

int characters[] = {119,38,149,28,73,136,86,199,8,225,36,222,129,63,27,102,41,227,39,113,90, 
             92,150,117,130,106,134,255,182,11,0,198,110,41,175,208,158,57,211,197}; 
 int i = 0; 
 NSString *result = [[NSString alloc] init]; 
 while ( i < 40) { 

     NSString *tempString = [NSString stringWithFormat:@"%c", characters[i]]; 
     result = [result stringByAppendingString:tempString]; 
     i++; 
 } 

 NSData *tempData = [result dataUsingEncoding:NSISOLatin1StringEncoding]; 
 NSString *b64String = [tempData base64EncodedStringWithOptions:0]; 
 NSLog(@"%@",b64String); 

With the above code, I get the result of b64String:

dyaVHEmIVscI4STegT8bZinjJ3FaXJZ1gmqG/7YLxm4pr9CeOdPF

but the correct answer should be:

dyaVHEmIVscI4STegT8bZinjJ3FaXJZ1gmqG/7YLAMZuKa/QnjnTxQ==

Can someone help me to get the right answer ?

  • I suspect the value 0 in your array to be the problem. 0 is often used as a string ending character and might not be formatted appropriately in your for loop (maybe even skipped). I would be curious to test it again with a 1 instead of a 0. – ForguesR Nov 16 '14 at 03:48
  • with a similar code in Python, I can get the correct answer. I want to know how to make it work in iOS. Because in my program, I need to base64 encode string, 99% my code work. with 1% chance, it will output the wrong answer. – Maki Cheung Nov 16 '14 at 04:01
  • To solve it we have to find the issue first. Please make a test by replacing the 0 with something else. If with that you get the same result in Python and in your code then we'll know what we have to fix. – ForguesR Nov 16 '14 at 04:04

1 Answers1

0

The problem is the value 0in your array. The value 0 is converted to char 0 which is a special character (null) used for string termination. Obviously iOS Objective-C treats this special character differently than Python and this is why you get a different result.

The null character should not be found in the middle of a "normal" string. You also have the ASCII code 8 (backspace) that shouldn't be found inside a "normal" string.

When removing those special characters you should get the same result.

Edit :

Just to make it clear, the problem is not the base64 encoding. Base64 can handle binary data without problem (and, of course, values 0 and 8). The problem is your conversion from integer values to char values.

I'm not an Objective-C expert but since you seems to be dealing with binary data you might be interested trying something like this :

Byte bytes[] = {119,38,149,28,73,136,86,199,8,225,36,222,129,63,27,102,41,227,39,113,90, 
         92,150,117,130,106,134,255,182,11,0,198,110,41,175,208,158,57,211,197}; 
NSData *tempData = [NSData dataWithBytes:bytes length:40];
NSString *b64String = [tempData base64EncodedStringWithOptions:0]; 
NSLog(@"%@",b64String);   
ForguesR
  • 3,558
  • 1
  • 17
  • 39
  • The problem is that : The server side use python3 to base64 encode and decode and I can't control the server side. At ios side, I need to base64 encode the string to connect to the server site. The ascii code 0 will sometimes occur. I need to find a way to do base64 encoding in ios as Python3 do. – Maki Cheung Nov 16 '14 at 05:01
  • I understand. You might want to deal with binary data instead. Please see my edit update. – ForguesR Nov 16 '14 at 05:36