Turning an NSString into an NSData

Discussion in 'Mac Programming' started by ausername, Apr 26, 2009.

  1. ausername macrumors newbie

    Joined:
    Feb 28, 2009
    #1
    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!
     
  2. pstoehr macrumors member

    Joined:
    Aug 5, 2003
    Location:
    Scheßlitz, Oberfranken, Bavaria, Germany
    #2
    Hi,

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

    Best regards
    Peter
     
  3. ausername thread starter macrumors newbie

    Joined:
    Feb 28, 2009
    #3
    Thanks! I think I know how I can make this work now, thanks a lot.
     
  4. ausername thread starter macrumors newbie

    Joined:
    Feb 28, 2009
    #4
    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.
     
  5. pstoehr macrumors member

    Joined:
    Aug 5, 2003
    Location:
    Scheßlitz, Oberfranken, Bavaria, Germany
    #5
    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
     
  6. ausername thread starter macrumors newbie

    Joined:
    Feb 28, 2009
    #6
    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?
     
  7. MrFusion macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #7
    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.
     
  8. ausername thread starter macrumors newbie

    Joined:
    Feb 28, 2009
    #8
    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...
     
  9. kpua macrumors 6502

    Joined:
    Jul 25, 2006
  10. ausername thread starter macrumors newbie

    Joined:
    Feb 28, 2009
    #10
    I don't really know how to use the NSData class, I'll go read up on that now.
     

Share This Page