Unable to convert NSData to NSString or any other format of informationElementData of CWNetwork

630 views Asked by At

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

1

There are 1 answers

3
Matthias Bauch On BEST ANSWER

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.