How to pass NSData as NSString and return it?

I have an NSData object.

I want to convert it to a string, go to a function, and then go back to the NSData object.

But how?

I tried this method, but NSData is different from the original!

here is my code:

// a generic class APClass *c = [[APClass alloc] init]; c.aNumber = 123; c.aString = @"my string"; // my data obj NSMutableData *data = [NSMutableData data]; // archiver to store class in nsdata NSKeyedArchiver *encoder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; [encoder encodeObject:[NSNumber numberWithInt:c.aNumber] forKey:@"aNum"]; [encoder encodeObject:c.aString forKey:@"aStr"]; [encoder finishEncoding]; [encoder release]; [c release]; NSLog(@"%@", data); NSString *d = [NSString stringWithFormat:@"%@", data]; // --- NSString *strFromData = [NSString stringWithFormat:@"%@", d]; NSData *dataNM = [strFromData dataUsingEncoding:NSUTF8StringEncoding]; // decoder to retrieve class from nsdata NSKeyedUnarchiver *decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:dataNM]; int number = [[decoder decodeObjectForKey:@"aNum"] intValue]; NSString *string = [decoder decodeObjectForKey:@"aStr"]; [decoder finishDecoding]; [decoder release]; NSLog(@"[Number: %d] -- [String: %@]", number, string); 

How can I convert back to source NSData?

data and dataNM vary in size. The compiler returns this error:

2012-04-02 16: 33: 28.269 DataTest [18008: f803] - [__ NSCFData objectForKey:]: unrecognized selector sent to instance 0x6b46c80

2012-04-02 16: 33: 28.270 DataTest [18008: f803] * Termination of the application to exclude the exception "NSInvalidArgumentException", reason: '- [__ NSCFData objectForKey:]: unrecognized selector sent to instance 0x6b46c80

thanks.

+6
source share
3 answers

solvable.

Using dataUsingEncoding , the NSData value is different.

To pass data around methods or applications, etc., I used base64 conversion.

Encode

 NSString *d = [NSString stringWithFormat:@"appdue://obj=%@", [APBase64Converter base64forData:data]]; 

Decode

 NSData *data = [APBase64Converter base64DataFromString:urlParams]; 

APBase64Converter is a lib that encodes / decodes data and strings easily.

A working example and APBase64Converter can be downloaded here: http://goo.gl/8YNjS

Thanks to everyone.

I "save" this post to help me and me next time!

+5
source

Use
NSData to NSString :
- (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding
and
NSString before NSData :
- (NSData *)dataUsingEncoding:(NSStringEncoding)encoding

with encoding
NSUTF8StringEncoding
or
NSMacOSRomanStringEncoding (which is 8-bit encoding).

0
source

Use Base64 class, it is available on github

  DatabaseResult* result = [self.database executeQuery:@"SELECT * FROM `images` WHERE image_id = ? AND imagetype = ?", self.routeId,self.imagetype]; for(EGODatabaseRow* row in result) { NSString *strImage=[row stringForColumn:@"image_photo"]; [Base64 initialize]; NSData *data = [Base64 decode:strImage]; UIImage *image=[UIImage imageWithData:data]; } 

This is how I convert String to data and then data to image.

-1
source

Source: https://habr.com/ru/post/912234/


All Articles