Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ausername

macrumors newbie
Original poster
Feb 28, 2009
25
0
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!
 
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
 
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:mad:"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:mad:"%@", 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.
 
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?
 
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.
 
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...
 
I don't really know how to use the NSData class, I'll go read up on that now.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.