简体   繁体   English

如何将NSString转换为NSData? 数据的内容与字符串相同

[英]how can I translate a NSString to NSData? And the data has the same content as the string

NSString * theString=@"e88d";
NSData * data;

// something I should implement

NSLog(@"%@", theString);

NSLog(@"%@",[data description]);

I want the results of the two printings to be the same. 我希望两次打印的结果相同。


AES encryption and decryption: AES加密和解密:

(1).The server: (1)。服务器:

If the plaintext is @"abcd"; 如果明文是@"abcd";

The AES encrypted data(NSData data type) is "d882830c dc892036 4345839f 13c7516a" AES加密data(NSData data type)"d882830c dc892036 4345839f 13c7516a"

(2).in my local app, my code is: (2)。在我的本地应用程序中,我的代码是:

NSData*data=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://..."]]; 
NSString * mystring= [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

However, to decrypt data successfully, I must have a data(NSData date type) which equals to "d882830c dc892036 4345839f 13c7516a" . 但是,要成功解密数据,我必须具有等于"d882830c dc892036 4345839f 13c7516a"data(NSData date type) But it is the mystring(NSString data type) not the data(NSData data type) that equals the right value. 但是,等于正确值的是mystring(NSString data type)而不是data(NSData data type)

The encryption and decryption function both need a data(NSData data type) as input datas. 加密和解密功能都需要一个data(NSData data type)作为输入数据。

- (NSData*)AES128EncryptWithKey:(NSString*)key;
- (NSData*)AES128DecryptWithKey:(NSString*)key;

I think this might answer your question 我认为这可能会回答您的问题

How do I convert a NSString value to NSData? 如何将NSString值转换为NSData?

the description you wanna set is not an instance specific value. 您想要设置的描述不是实例特定值。 It's the description of the class/object. 这是类/对象的描述。 NSData will have a description of like: 'this is a data object'. NSData的描述如下:“这是一个数据对象”。 You can override this value thou by overriding the method. 您可以通过覆盖方法来覆盖此值。


- (NSString *)description {
    return @"e88d"; //normally used for class description
}

Ofcourse you will have to inherit the NSData object for that and then override the description like code above. 当然,您必须为此继承NSData对象,然后覆盖上面的代码等描述。

PS. PS。 I dont think you wanna use description for this just explaining what the use of it is in every class. 我认为您不想为此使用描述,只是解释每个类中它的用途。


What you might want is: 您可能想要的是:


NSString * theString=@"e88d";
NSData * data=[theString dataUsingEncoding:NSUTF8StringEncoding];

NSLog(@"%@", theString);
NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
@interface NSString (Joke)
- (NSString *)description;
@end
@implementation NSString (Joke)
- (NSString *)description
{
    return @"Panda!";
}
@end

@interface NSData (Joke)
- (NSString *)description;
@end
@implementation NSData (Joke)
- (NSString *)description
{
    return @"Panda!";
}
@end

How about this 这个怎么样

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://..."]];
NSData *decryptedData = [data AES128DecryptWithKey:key];
NSString *mystring = [[NSString alloc] initWithData:decryptedData encoding:NSUTF8StringEncoding];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM