如何在ios中将base64字符串解码为NSString?

时间:2022-12-02 23:58:24

I have received some data in base64string format from JSON pasring in iphone. Now i want that data to store or decode in String. I have user Base64 class. It return me null value. so pls help me!!!

我从iphone中的JSON pasring收到了一些base64string格式的数据。现在我想要在String中存储或解码数据。我有用户Base64类。它返回null值。所以请帮助我!!!

2 个解决方案

#1


32  

I hope it will helpful to you

我希望它对你有所帮助

NSString *decodeString = @"Raja";

Encode String

NSData *encodeData = [decodeString dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [encodeData base64EncodedStringWithOptions:0];
NSLog(@"Encode String Value: %@", base64String);

Decode String

NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
NSLog(@"Decode String Value: %@", decodedString);  

#2


2  

Try to use like this..First you need to get this base64 library

尝试使用这样的..首先你需要得到这个base64库

https://github.com/l4u/NSData-Base64

And then import this file

然后导入此文件

   #import "NSData+Base64.h"

NSData *data = [NSData dataFromBase64String:baseString];

NSString *convertedString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

I hope it helps.

我希望它有所帮助。

#1


32  

I hope it will helpful to you

我希望它对你有所帮助

NSString *decodeString = @"Raja";

Encode String

NSData *encodeData = [decodeString dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [encodeData base64EncodedStringWithOptions:0];
NSLog(@"Encode String Value: %@", base64String);

Decode String

NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
NSLog(@"Decode String Value: %@", decodedString);  

#2


2  

Try to use like this..First you need to get this base64 library

尝试使用这样的..首先你需要得到这个base64库

https://github.com/l4u/NSData-Base64

And then import this file

然后导入此文件

   #import "NSData+Base64.h"

NSData *data = [NSData dataFromBase64String:baseString];

NSString *convertedString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

I hope it helps.

我希望它有所帮助。