0

I have a nsdata with bytes :0017c572 528e now i need to encode this byte using either UTF 8 of Ascii For this i have used following code in Objective C

 NSString *Str = [[NSString alloc] initWithData:value encoding:NSUTF8StringEncoding];

then later on at some point i need to get back the same bytes from Str for this i have used

  NSData *aData = [Str dataUsingEncoding:NSUTF8StringEncoding];
  NSLog(@"aData:%@", aData);

Now the problem is content of aData is null not 0017c572 528e . how can i do the this operation.

this concept works perfectly if data byte is : 323332

str = 232
aData = 323332
sra
  • 23,820
  • 7
  • 55
  • 89
Rajan Twanabashu
  • 4,586
  • 5
  • 43
  • 55
  • You need to work around zero bytes separately, zero is the string terminator so you have to encode it somehow, or store it in something which can handle zeros in the data. – tripleee Jan 04 '12 at 07:46

1 Answers1

0

Just print the Str value before the line

  NSLog(@"Str = %@",Str);
  NSData *aData = [Str dataUsingEncoding:NSUTF8StringEncoding];

It seems the Str variable does not have any value. Then only, it will return null. Please check.

Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55