Converting NSData to int

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

    Assuming _vertexData is NSData here and you know what data (types) to expect in your buffer you can iterate thru this block with NSData's .length property. In this Example each data block was 32 Bytes (storing 8 x float values) and i was interested in logging starting at the 5th float value

    float a,b,c,d; //prepare some values, no need to initialize
    
    // loop thru assuming 8 floats are stored after each other
    for (NSUInteger v = 0; v < _vertexData.length; v += sizeof(float)*8 ) {
    
        // set a starting point for the range, here the 5th float
        NSUInteger shift = v + (sizeof(float)*4);
    
        // store result in a..
        [_vertexData getBytes:&a range:NSMakeRange(shift,sizeof(a))];
    
        // increase the starting point by the size of data before
        shift += sizeof(a);
    
        [_vertexData getBytes:&b range:NSMakeRange(shift,sizeof(b))];
    
        shift += sizeof(b);
        [_vertexData getBytes:&c range:NSMakeRange(shift,sizeof(c))];
    
        shift += sizeof(c);
        [_vertexData getBytes:&d range:NSMakeRange(shift,sizeof(d))];
    
        fprintf(stderr, "r%f, g%f, b%f, a%f \n", a,b,c,d );
    }
    

    this could have been written much shorter, but for the sake of clarity and with less use of miss leading castings

    maybe this helps someone

提交回复
热议问题