Int from NSData

前端 未结 3 1770
借酒劲吻你
借酒劲吻你 2021-01-03 10:47

I have a NSData object with 4 bytes length. I know this 4 bytes construcuts a positive integer. How should I get the int from those NSdata object?

3条回答
  •  猫巷女王i
    2021-01-03 11:01

    If you know the endianness matches the current system:

    x = *(const UInt32 *)[theData bytes];
    

    If you know the endianness is backwards, follow that with:

    x = Endian32_Swap( x );
    

    If you are not sure of the source and target endianness, you should probably find another way to pass the data.

提交回复
热议问题