PDA

View Full Version : Turning an NSString into an NSData




ausername
Apr 26, 2009, 05:31 AM
Hello, I am trying to make something like this:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *myString = @"2c393430 35302c32";
NSLog(@"%@", myString);
const char *utfString = [myString UTF8String];
NSLog(@"%s", utfString);
NSData *data1 = [NSData dataWithBytes:utfString length:strlen(utfString)];
NSLog(@"%@", data1);
[pool drain];
return 0;
}

Outputs:

2009-04-26 06:27:27.606 test[27538:10b] 2c393430 35302c32
2009-04-26 06:27:27.607 test[27538:10b] 2c393430 35302c32
2009-04-26 06:27:27.608 test[27538:10b] <32633339 33343330 20333533 30326333 32>


The data is not the same as the string. I need a way to convert an NSString into an NSData without changing the contents... Or, a way to turn the NSData back into an NSString and restoring it to the original contents of the string. Any help would be great, thanks!



pstoehr
Apr 26, 2009, 06:12 AM
Hello, I am trying to make something like this:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *myString = @"2c393430 35302c32";
NSLog(@"%@", myString);
const char *utfString = [myString UTF8String];
NSLog(@"%s", utfString);
NSData *data1 = [NSData dataWithBytes:utfString length:strlen(utfString)];
NSLog(@"%@", data1);
[pool drain];
return 0;
}

Outputs:

2009-04-26 06:27:27.606 test[27538:10b] 2c393430 35302c32
2009-04-26 06:27:27.607 test[27538:10b] 2c393430 35302c32
2009-04-26 06:27:27.608 test[27538:10b] <32633339 33343330 20333533 30326333 32>


The data is not the same as the string. I need a way to convert an NSString into an NSData without changing the contents... Or, a way to turn the NSData back into an NSString and restoring it to the original contents of the string. Any help would be great, thanks!
Hi,

the data is the same!
The output of the NSData data is a hex-dump.
32hex = "2" ascii

Best regards
Peter

ausername
Apr 26, 2009, 06:16 AM
Thanks! I think I know how I can make this work now, thanks a lot.

ausername
Apr 26, 2009, 07:10 AM
Hmm, doesn't seem to be working. What I need to do is turn an NSImage into an NSData, and then turn the NSData into an NSString, and then turn the string back into data, and the data back into an image. This doesn't work:

// get an image from a file
NSImage *theImage = [NSImage imageNamed:@"anImage.png"];
// turn it into NSData
NSData *imageData = [theImage TIFFRepresentation];
// log data
NSLog(@"Image Data: %@", imageData);

// create a string with the data in it (is this a good way to do it?)
NSString *str = [NSString stringWithFormat:@"%@", imageData];
//log string
NSLog(@"The String: %@", str);

// now convert the string back into data
const char *urtfstring = [str UTF8String];
NSData *someData = [NSData dataWithBytes:urtfstring length:strlen(urtfstring)];
NSLog(@"The NSData formed from the string: %@", someData);

// turn data back into an image
NSBitmapImageRep *bits = [NSBitmapImageRep imageRepWithData:someData];
NSData *someMoreData = [bits representationUsingType: NSTIFFFileType properties: nil];
NSImage *aNewImage = [[NSImage alloc] initWithData:someMoreData];
// log the image
if (aNewImage == NULL)
NSBeep(); // this will beep because there is no image, can someone show me how to do this?

Output:
Image Data: <4d4d002a 00003758 ffffff00 ffffff00...etc
The String: <4d4d002a 00003758 ffffff00 ffffff00...etc
The NSData formed from the string: <3c346434 64303032 61203030 30303337 35382066 66666666 66303020...etc
*Beep*


As you can see the data from the string is not the same as the original data, and the image is NULL. Can anyone show me how to do this? Thanks.

pstoehr
Apr 26, 2009, 07:18 AM
Hi,

converting the TIFF representation of an image to a NSString doesn't make much sense to me.
What goal do you want to achive?

Best regards
Peter

ausername
Apr 26, 2009, 07:32 AM
That's the only way I know how to convert an image to an NSData.

My goal is to read in an image, convert it to an NSData, then convert the NSData to a string, so I can perform some operations on the string, and then convert it back into an NSImage. Is this possible?

MrFusion
Apr 26, 2009, 08:33 AM
That's the only way I know how to convert an image to an NSData.

My goal is to read in an image, convert it to an NSData, then convert the NSData to a string, so I can perform some operations on the string, and then convert it back into an NSImage. Is this possible?

What kind of operations do you want to perform on the image? Maybe someone knows a way to do that without having to convert it to a string. But if you don't tell what you want to do, then nobody can tell you how to do it.

ausername
Apr 26, 2009, 08:38 AM
I want to try encoding the string with a simple algorithm (a one for one swap)... encrypt the image I guess you could say. Not sure if it will work...

kpua
Apr 26, 2009, 10:24 AM
Can't you just do that on the raw bytes?

ausername
Apr 26, 2009, 10:25 AM
I don't really know how to use the NSData class, I'll go read up on that now.