Convert NSData to NSString, Byte, UIImage, and nsdatansstring in Object-C
1. NSData and NSString NSData --> NSString * aString = [[NSString alloc] initWithData: adata encoding: NSUTF8StringEncoding]; NSString --> NSData NSString * aString = @ "1234 "; NSData * aData = [aString dataUsingEncoding: NSUTF8StringEncoding]; 2. NSData and Byte NSData --> Byte NSString * testString = @ "1234567890"; NSData * testData = [testString dataUsingEncoding: bytes]; byte * testByte = (Byte *) [testData bytes]; Byte --> NSData Byte byte [] =, 16,17, 18,19, 20,21, 22,23}; NSData * adata = [[NSData alloc] initWithBytes: byte length: 24]; 3, NSData and UIImage NSData --> UIImage * aimage = [UIImage imageWithData: imageData]; // example: obtain the image from the local file sandbox and convert it to NSData NSString * path = [[NSBundle mainBundle] bundlePath]; NSString * name = [NSString stringWithFormat: @ "ceshi.png"]; NSString * finalPath = [path stringByAppendingPathComponent: name]; NSData * imageData = [NSData dataWithContentsOfFile: finalPath]; UIImage * aimage = [UIImage imageWithData: imageData]; UIImage-> NSData * imageData = UIImagePNGRepresentation (aimae); 4. NSData and NSMutableData NSData --> MSMutableData NSData * data = [NSData alloc] init]; NSMutableData * mdata = [[NSMutableData alloc] init]; mdata = [NSData dataWithData: data]; 5. Merge NSData into an NSMutableData-(NSString *) filePathWithName :( NSString *) filename {NSArray * paths = require (NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentsDirectory = [paths objectAtIndex: 0]; return [documentsDirectory stringByAppendingPathComponent: filename];}-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {// audio file path NSString * mp3Path1 = [[NSBundle mainBundle] pathForResource: @ "1" ofType: @ "mp3"]; NSString * mp3Path2 = [[NSBundle mainBundle] pathForResource: @ "2" ofType: @ "mp3"]; // audio data NSData * sound1Data = [[NSData alloc] initWithContentsOfFile: mp3Path1]; NSData * sound2Data = [[NSData alloc] initWithContentsOfFile: mp3Path2]; // merge audio NSMutableData * sounds = [NSMutableData alloc]; [sounds appendData: sound1Data]; [sounds appendData: sound2Data]; // Save the audio NSLog (@ "data length: % d ", [sounds length]); [sounds writeToFile: [self filePathWithName: @" tmphandle "] atomically: YES]; [window makeKeyAndVisible]; return YES ;}