朋友们,
它可能看起来像熟悉的问题,但我真的需要帮助将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); }但我得到Null或空响应。 请帮忙。
此致,议员
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
最满意答案
您无法将任意数据转换为字符串。 这仅适用于实际表示字符串的数据。 如果API公开NSData对象,通常不是这种情况。
要获得数据的某些含义,您必须知道数据代表什么。
您可以通过简单地查看它来获得一些结构。 如果我查看您发布的前几个字节,看起来数据结构良好而不是任意的。
数据接缝要拆分成数据包。 每个数据包都以类型标识符开头,后跟$ length字节。 然后会有$ length字节的数据
第一个数据包包含字符串“SYmantak”
00 08 53 79 6d 61 6e 74 61 6b ^^ Type Identifier ^^ Length ^^^^^^^^^^^^^^^^^^^^^^^ Data. In this case the ASCII string "SYmantak"如果你发现一堆字节都位于0x20和0x7E之间,你可能会看到ASCII。 这基本上是我如何计算出这个数据包的有效载荷。 因为我们有8个字节是ASCII,所以ASCII前面的0x08很可能意味着8个字节的数据。
下一个数据包看起来像这样:
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等等。 一般的数据包结构很容易解析。 虽然将这些字节转换为有意义的数据非常困难。 从其他数据包中可以看出,它并不总是像第一个包含ASCII的数据包一样容易。
但请不要将这种快速逆向工程结构视为理所当然。 我对这些领域的含义可能完全错了。
您应该尝试查找此数据的规范。 它应该在IEEE 802.11文档中的某处。
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 00and 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.
更多推荐
发布评论