title: How to pass NSData as NSString and get it back?
body: <p>i have a NSData object.  </p>
  
  <p><strong>I want to convert it to a string, pass to a function and then reconvert back to NSData object.</strong></p>
  
  <p>But How?</p>
  
  <p>I tried this method, but NSData value it's different from the original!</p>
  
  <p>here my code:</p>
  
  <pre><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);
  </code></pre>
  
  <p>How can i convert back to original NSData?  </p>
  
  <p>data and dataNM are different in size.
  Compiler give back this error:</p>
  
  <blockquote>
    <p>2012-04-02 16:33:28.269 DataTest[18008:f803] -[__NSCFData
    objectForKey:]: unrecognized selector sent to instance 0x6b46c80</p>
    
    <p>2012-04-02 16:33:28.270 DataTest[18008:f803] <em>*</em> Terminating app due
    to uncaught exception 'NSInvalidArgumentException', reason:
    '-[__NSCFData objectForKey:]: unrecognized selector sent to instance
    0x6b46c80'</p>
  </blockquote>
  
  <p>thanks.</p>