0

Friends,

It might look like familiar question but i really need help to convert NSData to any other understandable form. Basically i am using CoreWLAN framework and CWNetwork has properly called informationElement and it's data type is NSData. I have tried to convert it to any other readable format but not working. I have tried with all available string encoding. Below is sample code:

- (void) printNSData:(NSData *) dataToPrint forKey:(NSString *) key{
    for(int i = 1 ; i < 16 ; i++){
               size_t length = [dataToPrint length]+1;
        unsigned char aBuffer[length];
        [dataToPrint getBytes:aBuffer length:length];
        aBuffer[length] = 0;
        NSString *content = [[NSString alloc]  initWithBytes:aBuffer
                                                      length:[dataToPrint length] encoding: i];
        NSLog(@"%@ : %@ ", key,content);
    }
    /*
    NSUTF16BigEndianStringEncoding = 0x90000100,
    NSUTF16LittleEndianStringEncoding = 0x94000100,
    NSUTF32StringEncoding = 0x8c000100,
    NSUTF32BigEndianStringEncoding = 0x98000100,
    NSUTF32LittleEndianStringEncoding = 0x9c000100,
    NSProprietaryStringEncoding = 65536
     */
    NSString *content = [[NSString alloc]  initWithBytes:[dataToPrint bytes]
                                                  length:[dataToPrint length] encoding: NSUTF16BigEndianStringEncoding];
    NSLog(@"%@ : %@ ",key, content);
    content = [[NSString alloc]  initWithBytes:[dataToPrint bytes]
                                        length:[dataToPrint length] encoding: NSUTF16LittleEndianStringEncoding];
    NSLog(@"%@ : %@ ",key, content);

    content = [[NSString alloc]  initWithBytes:[dataToPrint bytes]
                                        length:[dataToPrint length] encoding: NSUTF32StringEncoding];
    NSLog(@"%@ : %@ ", key,content);

    content = [[NSString alloc]  initWithBytes:[dataToPrint bytes]
                                        length:[dataToPrint length] encoding: NSUTF32BigEndianStringEncoding];
    NSLog(@"%@ : %@ ", key,content);

    content = [[NSString alloc]  initWithBytes:[dataToPrint bytes]
                                        length:[dataToPrint length] encoding: NSUTF32LittleEndianStringEncoding];
    NSLog(@"%@ : %@ ", key,content);

    content = [[NSString alloc]  initWithBytes:[dataToPrint bytes]
                                        length:[dataToPrint length] encoding: NSProprietaryStringEncoding];
    NSLog(@"%@ : %@", key,content);

}

But i am getting either Null or empty response. Please please help.

Regards, MP

Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
M P
  • 352
  • 3
  • 14
  • What does the data look like? You cannot convert a sequence of arbitrary binary bytes to a string. The conversion fails if the byte sequence is not valid in the given encoding. – Martin R Sep 04 '13 at 11:03
  • informationElement is not a string. Not every NSData can be converted to a NSString. If `informationElement` would contain a NSString Apple would most likely have exposed it as NSString in the API. What exactly Information Element is should be specified in the IEEE 802.11 standard. – Matthias Bauch Sep 04 '13 at 11:03
  • as per the doc: Information element data included in beacon or probe response frames so i think these are bytes with some physical layer information. adding description of NSData in next comment.Here is the exact <00085379 6d616e74 616b0108 82840b16 2430486c 0301062a 01002f01 00301401 00000fac 04010000 0fac0401 00000fac 020c0032 040c1218 602d1a6e 181bff00 00000000 00000000 00000000 00000000 00000000 003d1606 – M P Sep 04 '13 at 12:01
  • 0d160000 00000000 00000000 00000000 00000000 00dd7f00 50f20410 4a000110 10440001 02104100 0100103b 00010310 470010cc af29d32e dd686efd 82ccfff1 ad91ff10 21000d4e 45544745 41522c20 496e632e 10230009 57475236 31347631 30102400 09574752 36313476 31301042 00053833 32353810 54000800 060050f2 04000110 11000957 47523631 34763130 10080002 0084dd09 00101802 02f00500 00dd1800 50f20201 01800003 a4000027 a4000042 435e0062 322f00dd 1e00904c 336e181b ff000000 00000000 00000000 00000000 00000000 000000dd 1a00904c 34060d16 00000000 00000000 00000000 00000000 000000> – M P Sep 04 '13 at 12:01
  • So how would you like to represent that data as a string? – Martin R Sep 04 '13 at 13:29
  • Basically i really don't know how this data will look like so what i did is used all encoding to get any representation but no luck. if you have MAC then you can also use "airport -s -x" and an xml plist will be shown of available networks. You can here see key name "IE" that is same as informationElement of CWNetwork. Thanks for you help and replies. So i am stuck here (basically looking for some info and thinking it might have it) – M P Sep 04 '13 at 13:49
  • I don't believe that's character data. Assuming you didn't somehow garble in in transmission, you need to know what the layout of the data is and interpret it the context of that layout. – Hot Licks Sep 04 '13 at 15:43

1 Answers1

0

You can't convert arbitrary data into a string. That only works for data that actually represents a string. Which is usually not the case if an API exposes a NSData object.

To get some meaning into the data you have to know what the data represents.

You might be able to get some structure into it by simply looking at it.
If I look at the first few bytes you have posted it looks like the data is well structured and not arbitrary.

The data seams to be split into packets. Each packet starts with a type identifier, which is followed by a $length byte. And then there will be $length bytes of data

The first packet contains the string "SYmantak"

00 08 53 79 6d 61 6e 74 61 6b
^^ Type Identifier
   ^^ Length
      ^^^^^^^^^^^^^^^^^^^^^^^ Data. In this case the ASCII string "SYmantak"

If you find a bunch of bytes that all lay between 0x20 and 0x7E you are probably looking at ASCII. That's basically how I figured out the payload of this packet. And because we have 8 bytes that are ASCII the 0x08 in front of the ASCII most likely means 8 bytes of data.

The next packets look like this:

01 08 82 84 0b 16 24 30 48 6c
^^ Type Identifier
   ^^ Length
      ^^^^^^^^^^^^^^^^^^^^^^^ Data. But not a ASCII string
03 01 06
2a 01 00
2f 01 00
30 14 01 00 00 0f ac 04 01 00 00 0f ac 04 01 00 00 0f ac 02 0c 00 
32 04 0c 12 18 60 
2d 1a 6e 18 1b ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

and so on. The general packet structure is quite easy to parse.
Though it will be very hard to turn these bytes into meaningful data. As you can see from the other packets, it's not always as easy as with the first packet that contained ASCII.

But please don't take this quickly reverse engineered structure for granted. I might be completely wrong about the meaning of these fields.

You should try to find the specification of this data. It should be somewhere in the IEEE 802.11 documents.

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
  • Thanks so much!! You explained very well and yes "Symantak" is SSID of network. I will look into other ways to decode it and will also look into IEEE specs. – M P Sep 04 '13 at 18:53
  • I was able to get the details of this raw data. I also verified using packet analysis (wireshark) and i found that 01 08 82 84 0b 16 24 30 48 6c is basically 01=second segment, 08=again length and 82 84 0b 16 24 30 48 6c is 1,2,5.511,18,24,36,54 respectively. I was also able to get these bytes from NSData but then when i used c code to convert to meaningful text it is showing like 08 82 84 0B 16 24 30 48 and this is how i converted (next comment) – M P Sep 06 '13 at 03:58
  • - (void) tempPrint:(int) length bufferToPrint: (unsigned char *) buffer{ char* buf_str = (char*) malloc (2*length + 1); char* buf_ptr = buf_str; for (int i = 0; i < length; i++) { buf_ptr += sprintf(buf_ptr, "%02X ",(unsigned char) buffer[i]); } sprintf(buf_ptr,"\n"); *(buf_ptr + 1) = '\0'; printf("%s\n", buf_str); } but not working.....any help – M P Sep 06 '13 at 04:00