在NSData和base64字符串之间转换 [英] Converting between NSData and base64 strings

查看:1275
本文介绍了在NSData和base64字符串之间转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NSData 和base64字符串之间进行转换的最简单,最快的代码是什么?我已经在SO上阅读了大量的解决方案,而且大多数都涉及添加另一个类等。我找到了一个很好的解决方案这里但它太复杂了。

What is the easiest and fastest code to do a conversion between NSData and a base64 string? I've read a bunch of solutions at SO and mostly they involve in adding another class etc. I found a great solution here but it's too complex.

推荐答案

向下滚动到结论您链接的页面部分 >并下载提供的NSData + Base64文件。它是迄今为止我见过的最好的解决方案,非常容易使用。如果您可以了解有关Cocoa的任何信息,您可以学习使用该项目。

Scroll down to the Conclusion section on the page you linked and download the provided NSData+Base64 files. Its the best solution I have seen so far and is incredibly easy to use. If you can learn anything about Cocoa, you can learn to use that project.

示例

NSString *originalString = [NSString stringWithFormat:@"test"]; 
NSData *data = [NSData dataFromBase64String:originalString];  
NSLog([data base64EncodedString]); 

以上将在将原始字符串转换为base64并返回正常的未编码字符串后打印出来。

The above will print out the original string after converting it to base64 and back to a normal unencoded string.

这篇关于在NSData和base64字符串之间转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆