ios 将 NSData 转换为字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6428461/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:27:44  来源:igfitidea点击:

Convert NSData to String?

iphoneiossqliteopensslnsdata

提问by Zach

I am storing a openssl private Key EVP_PKEY as nsdata. For this I am serializing into a byte stream using the code below

我将 openssl 私钥 EVP_PKEY 存储为 nsdata。为此,我使用下面的代码序列化为字节流

unsigned char *buf, *p;
int len;
len = i2d_PrivateKey(pkey, NULL);
buf = OPENSSL_malloc(len); 
p = buf;
i2d_PrivateKey(pkey, &p);

where pkey is of type EVP_PKEY. Then I am storing the bytes from buffer 'p' as an NSData using the line given below

其中 pkey 的类型为 EVP_PKEY。然后我使用下面给出的行将缓冲区“p”中的字节存储为 NSData

NSData *keydata = [NSData dataWithBytes:P length:len];

Now I am converting it to a NSString using the code given below but when i print it into console its giving some other characters.

现在我使用下面给出的代码将它转换为 NSString,但是当我将它打印到控制台时,它给出了一些其他字符。

NSString *content =[ NSString stringWithCString:[keydata bytes] encoding:NSUTF8StringEncoding];

Could someone help?

有人可以帮忙吗?

Basically I want to store the EVP_PKEY into a sqlite database

基本上我想将 EVP_PKEY 存储到 sqlite 数据库中

am I on the right track? Thanks.

我在正确的轨道上吗?谢谢。

回答by louiscoquio

Objective-C

目标-C

You can use (see NSString Class Reference)

您可以使用(见NSString类参考

- (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding

Example:

例子:

NSString *myString = [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding];

Remark: Please notice the NSDatavalue must be valid for the encoding specified (UTF-8 in the example above), otherwise nilwill be returned:

备注:请注意该NSData值必须对指定的编码(上例中的 UTF-8)有效,否则nil将返回:

Returns nil if the initialization fails for some reason (for example if data does not represent valid data for encoding).

如果由于某种原因初始化失败(例如,如果数据不代表有效的编码数据),则返回 nil。

Prior Swift 3.0

之前的 Swift 3.0

String(data: yourData, encoding: NSUTF8StringEncoding)

Swift 3.0

斯威夫特 3.0

String(data: yourData, encoding: .utf8)

See String#init(data:encoding:) Reference

参见String#init(data:encoding:) 参考

回答by pierre23

Prior Swift 3.0 :

Swift 3.0 之前的版本:

String(data: yourData, encoding: NSUTF8StringEncoding)

For Swift 4.0:

对于 Swift 4.0:

String(data: yourData, encoding: .utf8)

回答by aspergillusOryzae

I believe your "P" as the dataWithBytes param

我相信你的“P”作为 dataWithBytes 参数

NSData *keydata = [NSData dataWithBytes:P length:len];

should be "buf"

应该是“buf”

NSData *keydata = [NSData dataWithBytes:buf length:len];

since i2d_PrivateKey puts the pointer to the output buffer p at the end of the buffer and waiting for further input, and buf is still pointing to the beginning of your buffer.

因为 i2d_PrivateKey 将指向输出缓冲区 p 的指针放在缓冲区的末尾并等待进一步的输入,而 buf 仍指向缓冲区的开头。

The following code works for me where pkey is a pointer to an EVP_PKEY:

以下代码对我有用,其中 pkey 是指向 EVP_PKEY 的指针:

unsigned char *buf, *pp;
int len = i2d_PrivateKey(pkey, NULL);
buf = OPENSSL_malloc(len); 
pp = buf;
i2d_PrivateKey(pkey, &pp);

NSData* pkeyData = [NSData dataWithBytes:(const void *)buf length:len];
DLog(@"Private key in hex (%d): %@", len, pkeyData);

You can use an online converter to convert your binary data into base 64 (http://tomeko.net/online_tools/hex_to_base64.php?lang=en) and compare it to the private key in your cert file after using the following command and checking the output of mypkey.pem:

您可以使用在线转换器的二进制数据转换成基64(http://tomeko.net/online_tools/hex_to_base64.php?lang=en在你的证书文件中使用以下命令后),并把它比作私钥和检查 mypkey.pem 的输出:

openssl pkcs12 -in myCert.p12 -nocerts -nodes -out mypkey.pem

I referenced your question and this EVP function sitefor my answer.

我参考了你的问题和这个 EVP 功能站点作为我的答案。

回答by Travis M.

Swift 3:

斯威夫特 3:

String(data: data, encoding: .utf8)

回答by Billy

A simple way to convert arbitrary NSData to NSString is to base64 encode it.

任意的NSData转换为NSString的一个简单的方法是Base64编码它。

NSString *base64EncodedKey = [keydata base64EncodedStringWithOptions: NSDataBase64Encoding64CharacterLineLength];

You can then store it into your database for reuse later. Just decode it back to NSData.

然后,您可以将其存储到您的数据库中以供以后重用。只需将其解码回 NSData。

回答by Alexander Volkov

Swift:

迅速:

String(data: data!, encoding: NSUTF8StringEncoding)