Some php encoded utf-8 base64 strings cannot be decoded using the iOS base64 library?

Here is one piece of Chinese utf-8 text that is encoded by PHP on the server side, but when I decode it using iOS, it returns null.

I also tried this interactive tool where the text can be decoded well.

  NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:content options:0];
  content = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];

5aW96ZuF77yM5ZKx5p2l5LiA5L + X55qE77yM5pS56Ieq5Lic5Y2X6KW / 5YyX6aOO44CCCuS4juS9oOebuOmAou + 8jOWFqOaYr + acuue8mOW3p + WQiOOAguWPr + Wtpui1t + adpeWNtOW / g + aGlOaCtOOAggrmgLvmmK / ovpPkuoborqnlho3ljrvlrabvvIzlrabkuobo

Here is the test code to debug this problem using xcode:

    NSString * = @"5aW96ZuF77yM5ZKx5p2l5LiA5L+X55qE77yM5pS56Ieq5Lic5Y2X6KW/5YyX6aOO44CCCuS4juS9oOebuOmAou+8jOWFqOaYr+acuue8mOW3p+WQiOOAguWPr+Wtpui1t+adpeWNtOW/g+aGlOaCtOOAggrmgLvmmK/ovpPkuoborqnlho3ljrvlrabvvIzlrabkuobo";

//
    NSData *decodedData = [[NSData alloc] initWithBase64EncodedString: options:0];
    NSString *content = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding] ;

    NSLog(content);

Here is the screen of xcode, and where seems the string have been decoded to NSData, but fail to be converted to utf8 string.

here is the decoded result by online tool

+4
source share
2 answers

base64:

5aW96ZuF77yM5ZKx5p2l5LiA5L + X55qE77yM5pS56Ieq5Lic5Y2X6KW/5YyX6aOO44CCCuS4juS9oOebuOmAou + 8jOWFqOaYr + acuue8mOW3p + WQiOOAguWPr + Wtpui1t + adpeWNtOW/ + aGlOaCtOOAggrmgLvmmK/ovpPkuoborqnlho3ljrvlrabvvIzlrabkuobo

, , =/== . , , initWithBase64EncodedString :

e5a5bde9 9b85efbc 8ce592b1 e69da5e4 b880e4bf 97e79a84 efbc8ce6 94b9e887 
aae4b89c e58d97e8 a5bfe58c 97e9a38e e380820a e4b88ee4 bda0e79b b8e980a2 
efbc8ce5 85a8e698 afe69cba e7bc98e5 b7a7e590 88e38082 e58fafe5 ada6e8b5 
b7e69da5 e58db4e5 bf83e686 94e682b4 e380820a e680bbe6 98afe8be 93e4ba86 
e8aea9e5 868de58e bbe5ada6 efbc8ce5 ada6e4ba 86e8

, UTF8. , http://base64decode.net , , UTF8 ( , - ). , , , , 学了 (, , , - base64).

, UTF8 e4 ba 86. , 64, , e8. , e8, , UTF8. , base64 200 , , UTF8 .


base64:

5aW96ZuF77yM5ZKx5p2l5LiA5L + X55qE77yM5pS56Ieq5Lic5Y2X6KW/5YyX6aOO44CCCuS4juS9oOebuOmAou + 8jOWFqOaYr + acuue8mOW3p + WQiOOAguWPr + Wtpui1t + adpeWNtOW/ + aGlOaCtOOAggrmgLvmmK/

base64. , 163 , . base64, - .

, =, 164 , base64:

5aW96ZuF77yM5ZKx5p2l5LiA5L + X55qE77yM5pS56Ieq5Lic5Y2X6KW/5YyX6aOO44CCCuS4juS9oOebuOmAou + 8jOWFqOaYr + acuue8mOW3p + WQiOOAguWPr + Wtpui1t + adpeWNtOW/ + aGlOaCtOOAggrmgLvmmK/=

= , base64. , base64-decoded :

好雅,咱来一俗的,改自东南西北风。
与你相逢,全是机缘巧合。可学起来却心憔悴。
总是

, ?

, ? , , , , base64 , .

= == 64 . wikiipedia base64.

+3

@Rob .

NSData base64

JSON "=" "==", base64decode. , base64string "=" .

+1

Source: https://habr.com/ru/post/1537164/


All Articles