Converting NSData to int

后端 未结 7 1240
被撕碎了的回忆
被撕碎了的回忆 2020-12-08 05:50

I\'ve an NSData object, the length is 4 bytes .These four bytes i\'m extracting from another NSData object using ,

fourByteData=[completeData subdataWithRang         


        
7条回答
  •  南笙
    南笙 (楼主)
    2020-12-08 06:12

    It depends on the Endianness notation of the data you want to convert, in relation to your device Endianness notation. wiki on Endianness

    To keep it simple you need to check does two method

    NSData *data4 = [completeData subdataWithRange:NSMakeRange(0, 4)];
    int value = CFSwapInt32BigToHost(*(int*)([data4 bytes]));
    

    or

    NSData *data4 = [completeData subdataWithRange:NSMakeRange(0, 4)];
    int value = CFSwapInt32LittleToHost(*(int*)([data4 bytes]));
    

    And check which one make more sense when you parse the data.

提交回复
热议问题