2

次の方法でNSDataを受信して​​います

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

サーバーは次の形式でデータを送信します

                 04 01 00  

ヘキサ値として。したがって、このデータをchar配列に変換して、すべてのペアに個別にアクセスできるようにする必要があります。

4

1 に答える 1

1

バイトごとに比較したい場合は、次のようにできます。

    //NSData *test; // assume this is your NSData containing 0x04 0x01 0x00

    char *ptr = (void *)[test bytes]; // set a pointer to the beginning of your data bytes
    if(*ptr == 0x04) {
        NSLog(@"okay,.. got a 0x04");
    }
    ptr++; // go to the next byte
    if(*ptr == 0x01) {
        NSLog(@"okay,.. got a 0x01");
    }

それがうまくいくことを願っています。

于 2012-04-18T18:53:29.280 に答える