Iphone – how to convert NSData to char array containing hexa values

iphone

i am receiving NSData by following method

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

server send data in follwing format

                 04 01 00  

as hexa values. so now need to convert this data into char array so that i can access every pair separately
please help

Best Answer

You can go like this if you'd like to compare byte by byte:

    //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");
    }

hope that does work for you.

Related Topic