开发者

Int from NSData

开发者 https://www.devze.com 2023-01-04 05:22 出处:网络
I have a NSData object with 4 bytes length. I know this 4 byte开发者_Go百科s construcuts a positive integer. How should I get the int from those NSdata object?If you know the endianness matches the cu

I have a NSData object with 4 bytes length. I know this 4 byte开发者_Go百科s construcuts a positive integer. How should I get the int from those NSdata object?


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.


int32_t value; // make unsigned if needed. 
[myData getBytes: &value];


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消