2

I am trying to pass a NSNumber into a UInt32, which is working.. Then I am trying to stuff that UInt32 into a NSData object.. however this is where things get abit funky...

when I try to write whats in that NSData object out to a string its returning (null).

This is what my code looks like, I think it has something to do with the way I am passing CACHE_VALUE into requestCacheData.. but I am not totally sure why.

// Use the correctly returned cache number
    UInt32 CACHE_VALUE = [cacheNumber intValue];
    NSLog(@"%lu", CACHE_VALUE); //gives me the correct integervalue
    NSData * requestCacheData = [[NSData alloc] initWithBytes:&CACHE_VALUE length:sizeof(CACHE_VALUE)];

    NSString *myString = [[NSString alloc] initWithData:requestCacheData encoding:NSUTF8StringEncoding];
    NSLog(@"%@", myString); //outputs (null)

any help would be appreciated.

C.Johns
  • 10,185
  • 20
  • 102
  • 156

1 Answers1

0

you can't turn the NSData to a right NSString that NSData do not contain a right string content.

in your case , your NSData just contain a binary unsigned int,

what do you suppose to get ?

NSString* str= @"a NSString";
NSData* requestCacheData =[str dataUsingEncoding:NSUTF8StringEncoding];
NSString *myString = [[NSString alloc] initWithData:requestCacheData encoding:NSUTF8StringEncoding];
NSLog(@"%@", myString); 

you may get the right ouput,

but if you want to turn a UINT to NSString

just

NSString *str = [[[NSString alloc] initWithFormat:@"%u",CACHE_VALUE] autorelease];
adali
  • 5,977
  • 2
  • 33
  • 40