Int from NSData

前端 未结 3 1771
借酒劲吻你
借酒劲吻你 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条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-03 11:06

    I have found the solution here.

    char buffer[4]; 
    
    [data getBytes:buffer length:4]; 
    
    int dataSize = 0; 
    
    char cBuf2[4]; 
    
    for(int k=0;k < 4; ++k) {
    
     cBuf2[k] = buffer[3-k]; 
    
    } 
    
    memcpy(&dataSize, cBuf2, 4); 
    
    NSLog(@"Zip stream size :%d", dataSize);
    

提交回复
热议问题