Converting NSdata to bytes & then converting the first n bytes to int

后端 未结 3 1799
谎友^
谎友^ 2021-02-03 16:36

I\'ve a NSData object. First I want to convert NSData object to bytes, then read the first four bytes in this NSData object and then conve

3条回答
  •  粉色の甜心
    2021-02-03 17:06

    That's quite simple. Use bytes to get at the bytes and then cast to unsigned char*

    unsigned char *n = [yourNSData bytes];
    int value1 = n[0];
    int value2 = n[1];
    int value3 = n[2];
    int value4 = n[3];
    

    Update

    To turn this into a single int assumes bytes contains a valid int:

    int result = *(int *)n;
    

提交回复
热议问题