将base 64转换为NSString

时间:2012-09-07 06:12:37

标签: ios objective-c nsstring base64

我的XML采用base64编码格式。我想将其转换为NSString对象。我怎么能这样做?

  

PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPHN1cnZleSB4bWxuczphYmFw   c3VydmV5PSJodHRwOi8vd3d3LnNhcC5jb20vYWJhcHN1cnZleSIgeG1sbnM6YmVlPSJodHRwOi8v   d3d3LnNhcC5jb20vc3VydmV5L2JlZSIgeG1sbnM6Zm89Imh0dHA6Ly93d3cudzMub3JnLzE5OTkv   WFNML0Zvcm1hdCIgeG1sbnM6aHRtbGI9Imh0dHA6Ly93d3cuc2FwLmNvbS9zdXJ2ZXkvaHRtbGIi   IHhtbG5zOm91dD0iaHR0cDovL3d3dy53My5vcmcvMTk5OS9YU0wvT3V0cHV0IiB4bWxuczpzdnk9   Imh0dHA6Ly93d3cuc2FwLmNvbS9zdXJ2ZXkvc3Z5IiB4bWxuczp0bXA9Imh0dHA6Ly93d3cuc2Fw   LmNvbS9zdXJ2ZXkvdG1wIiB4bWxuczp2YWx1ZXM9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvWFNM   L1RyYW5zZm9ybVZhbHVlcyIgeG1sbnM6d2ZmPSJodHRwOi8vd3d3Lm15c2FwLmNvbS93ZmYvMjAw   MSI + PHJhdGluZ2ZhY3Rvcj48cTE + MSA8L3ExPjxpZF8zNmYyN2YwY2UxNDY2MDRlOTIyMzY4Mjcy   MWFhMjc5Yj4wIDwvaWRfMzZmMjdmMGNlMTQ2NjA0ZTkyMjM2ODI3MjFhYTI3OWI + PGlkX2Y3YWMx   ODM4MDliNjAxNDE4MWVlNGEwNDIyNGYxNTNlPjAgPC9pZF9mN2FjMTgzODA5YjYwMTQxODFlZTRh   MDQyMjRmMTUzZT48L3JhdGluZ2ZhY3Rvcj48cmF0aW5nPjxxMT48YTEgcmF0aW5nPSIxICI + aWRf   ZTE1ZjhhNjQ5OTY3NDg0ZWI1MzUzZ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lYjUzNTNmYjk3MjFhZjc1ZjwvYTE + PC9x   MT48aWRfMzZmMjdmMGNlMTQ2NjA0ZTkyMjM2ODI3MjFhYTI3OWI + PGlkXzgwMTVlNmQ0ZGRmYjky   NDE4ODcxMWIzMGE5MWQzZGU4IE11bHRpcGxlQ2hvaWNlPSJ0cnVlIj4gPC9pZF84MDE1ZTZkNGRk   ZmI5MjQxODg3MTFiMzBhOTFkM2RlOD48L2lkXzM2ZjI3ZjBjZTE0NjYwNGU5MjIzNjgyNzIxYWEy   NzliPjxpZF9mN2FjMTgzODA5YjYwMTQxODFlZTRhMDQyMjRmMTUzZT48aWRfMjBhMzg0OWIwMDEw   Yjk0YWFhNGJhNDEwYTRkNzY2ZWI + PC9pZF8yMGEzODQ5YjAwMTBiOTRhYWE0YmE0MTBhNGQ3NjZl   Yj48L2lkX2Y3YWMxODM4MDliNjAxNDE4MWVlNGEwNDIyNGYxNTNlPjwvcmVzdWx0PjxyZXBsYWNl   YWJsZXMvPjxzeXN0ZW1wYXJhbWxvY2F0aW9uPmh0dHA6Ly9TdXJ2ZXlSZXAvQ1JNX1NWWV9CU1Bf   U1lTVEVNUEFSQU1fSEFSSVRfVEVTVC5YTUw8L3N5c3RlbXBhcmFtbG9jYXRpb24 + PGhpZGRlbi8 +   PHJlYWRPbmx5Lz48bWFuZGF0b3J5Lz48bm9EaXNwbGF5Lz48YWRkSXRlbXMvPjxzY3JpcHQvPjxv   bkV2ZW50Lz48L3N1cnZleT4 =

1 个答案:

答案 0 :(得分:2)

谷歌的第二个结果就是:

- (NSString *)base64Encode:(NSString *)plainText
{
  NSData *plainTextData = [plainText dataUsingEncoding:NSUTF8StringEncoding];
  NSString *base64String = [plainTextData base64EncodedString];
  return base64String;
}
 
- (NSString *)base64Decode:(NSString *)base64String
{
  NSData *plainTextData = [NSData dataFromBase64String:base64String];
  NSString *plainText = [[NSString alloc] initWithData:plainTextData encoding:NSUTF8StringEncoding];
  return plainText;
}

http://agerson.net/base64-encode-and-decode-nsstring-cocoa