0

朋友们,

这可能看起来像一个熟悉的问题,但我真的需要帮助将 NSData 转换为任何其他可理解的形式。基本上我使用的是CoreWLAN框架,CWNetwork已经正确调用了informationElement,它的数据类型是NSData。我试图将其转换为任何其他可读格式,但无法正常工作。我已经尝试过所有可用的字符串编码。下面是示例代码:

- (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);

}

但我得到空或空响应。请帮忙。

问候,议员

4

1 回答 1

0

您不能将任意数据转换为字符串。这仅适用于实际代表字符串的数据。如果 API 公开 NSData 对象,通常情况并非如此。

要使数据有意义,您必须知道数据代表什么。

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.

于 2013-09-04T15:20:23.497 回答