问题描述
我将openssl私钥EVP_PKEY存储为nsdata。为此我使用下面的代码序列化为字节流
I am storing a openssl private Key EVP_PKEY as nsdata. For this I am serializing into a byte stream using the code below
unsigned char *buf, *p;
int len;
len = i2d_PrivateKey(pkey, NULL);
buf = OPENSSL_malloc(len);
p = buf;
i2d_PrivateKey(pkey, &p);
其中pkey的类型为EVP_PKEY。
然后我使用下面给出的行将缓冲区'p'中的字节存储为NSData
where pkey is of type EVP_PKEY.Then I am storing the bytes from buffer 'p' as an NSData using the line given below
NSData *keydata = [NSData dataWithBytes:P length:len];
现在我使用下面给出的代码将其转换为NSString但是当我将其打印到控制台时给出一些其他字符。
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 *content =[ NSString stringWithCString:[keydata bytes] encoding:NSUTF8StringEncoding];
有人可以帮忙吗?
我走在正确的轨道上吗?
谢谢。
am I on the right track?Thanks.
推荐答案
Objective-C
您可以使用(参见)
- (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding
示例:
NSString *myString = [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding];
备注:请注意 NSData
value必须对指定的编码有效(上例中为UTF-8),否则将返回 nil
:
Remark: Please notice the NSData
value must be valid for the encoding specified (UTF-8 in the example above), otherwise nil
will be returned:
先前的Swift 3.0
String(data: yourData, encoding: NSUTF8StringEncoding)
Swift 3.0
String(data: yourData, encoding: .utf8)
请参阅
这篇关于将NSData转换为String?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!