Converting NSData to int

后端 未结 7 1194
被撕碎了的回忆
被撕碎了的回忆 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:16

    That statement would give you the first 16 bytes of data, not 4. To get the first 4 bytes you need to modify your statement to:

    fourByteData = [completeData subdataWithRange:NSMakeRange(0, 4)];
    

    To read the data from the NSData Object to an integer you could do something like:

    int theInteger;
    [completeData getBytes:&theInteger length:sizeof(theInteger)];
    

    You do not need to get the first 4 bytes if you are just converting it to an integer. You can do this directly from the two lines above and your completeData receiver

    0 讨论(0)
提交回复
热议问题