0

I am receiving some byte data from server as defined in following method

     - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
      {

      \//04 01 00
NSString *string = [
                    [NSString alloc] 
                    initWithData:data
                    encoding:NSASCIIStringEncoding
                    ];
NSLog(string);

char *ptr = (void *)[data bytes]; // set a pointer to the beginning of your data bytes

if(*ptr == 0x06) {
    NSLog(@"okay,.. got a  0x06");
}
ptr++; // go to the next byte
if(*ptr == 0x05) {
    NSLog(@"okay,.. got a 0x05");
}
ptr++;
if(*ptr==0X00){
    NSLog(@"okay,.. got a 0X00");

}


//04 01 00
NSString* newStr = [[NSString alloc] initWithData:data
                                         encoding:NSASCIIStringEncoding];
///////////////////
///////////////////
NSLog(@"I received some thing , now trying to read it");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Data Received" message:newStr delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
[alert show];
[alert release];

 [sock readDataWithTimeout:-1 tag:0];
//NSLog(@"Reading %@",newStr);

}

I can compare the hexa values but not able to see in UIAlertView as I think I am not able to convert correctly , so please help how to convert in into NSString and in NSMutableArray so that I can use it dynamically

Ali
  • 10,774
  • 10
  • 56
  • 83
  • What do you expect the UIAlertView to display? The hex representation of the data? – David Gelhar Apr 22 '12 at 16:01
  • yes , also i am not able to see data using NSLog, i have compared data by hard code , i want to compare as in array – Ali Apr 22 '12 at 16:39
  • If you want to see the hex representation of the raw binary data, you'll need to explicitly convert it, as in [How to convert an NSData into an NSString Hex string?](http://stackoverflow.com/questions/7520615/how-to-convert-an-nsdata-into-an-nsstring-hex-string) – David Gelhar Apr 22 '12 at 16:43

2 Answers2

0

I didn't try your code, but not all ascii characters can be represented visually. Try with some codes with ascii code >32, look for letters or numbers. Or echo the characters as decimal or hexa numbers.

Templar
  • 1,694
  • 1
  • 14
  • 32
0

Try this,

   NSString* newStr = [[NSString alloc] initWithData:data
                                     encoding:NSUTF8StringEncoding];
rakeshNS
  • 4,227
  • 4
  • 28
  • 42