ios 在 NSData 和 base64 之间转换?字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5999370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting between NSData and base64?strings
提问by aherlambang
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 herebut it's too complex.
在NSData
和 base64 字符串之间进行转换的最简单和最快的代码是什么?我在 SO 上阅读了一堆解决方案,其中大部分涉及添加另一个类等。我在这里找到了一个很好的解决方案,但它太复杂了。
回答by Ryan Wersal
Scroll down to the Conclusionsection on the page you linkedand 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.
向下滚动到您链接的页面上的结论部分并下载提供的 NSData+Base64 文件。它的最好的解决方案我迄今所看到的,是非常容易使用。如果您可以了解有关 Cocoa 的任何信息,您就可以学习使用该项目。
Example
例子
NSString *originalString = [NSString stringWithFormat:@"test"];
NSData *data = [NSData dataFromBase64String:originalString];
NSLog([data base64EncodedString]);
The above will print out the original string after converting it to base64 and back to a normal unencoded string.
上面将在将原始字符串转换为 base64 并返回到正常的未编码字符串后打印出原始字符串。
回答by Sixten Otto
As of iOS 7, NSData
now directly provides this functionality with the new methods -base64EncodedDataWithOptions:
and -base64EncodedStringWithOptions:
. (The options let you specify that the string is/should be line-wrapped, the better to deal with email, and user-facing displays.)
从 iOS 7 开始,NSData
现在直接通过新方法-base64EncodedDataWithOptions:
和-base64EncodedStringWithOptions:
. (选项让您指定字符串是/应该换行,更好地处理电子邮件和面向用户的显示。)
回答by kraag22
You don't need any custom implementation. Creating base64 from NSData is shown in other answers. There is opposite direction. From Base64 string to NSData:
您不需要任何自定义实现。从 NSData 创建 base64 显示在其他答案中。有相反的方向。从 Base64 字符串到 NSData:
NSString *base64Encoded = @"some base64 string";
NSData *nsdataFromBase64String = [[NSData alloc] initWithBase64EncodedString:base64Encoded options:0];
回答by meaning-matters
Be aware that there are more Base64 formats.
请注意,还有更多 Base64 格式。
For example JWTs use a URL safe format.
例如,JWT 使用URL 安全格式。
回答by SeeCoolGuy
I ended up using this same class as provided by SUDZC
我最终使用了与 SUDZC 提供的相同的类
implementation was easy first I did an import
实施很容易首先我做了一个导入
#import "NSData+Base64.h"
then I was able to call my data.
然后我就可以调用我的数据了。
NSData *data = [[NSData alloc] initWithData:[NSData dataWithBase64EncodedString:strData]];
回答by Stephan
Or you may take a look to the (quite new) CryptoCompatibilitysample project, I think there is a wrapper class for base64 operation. It is a MacOS sample but it uses the library libresolve.dylib with I think is available on iOS too (is see it at least here in iOS7).
或者您可以查看(相当新的)CryptoCompatibility示例项目,我认为有一个用于 base64 操作的包装类。这是一个 MacOS 示例,但它使用库 libresolve.dylib,我认为在 iOS 上也可用(至少在 iOS7 中可以看到它)。