开发者

iphone NSMutableData bytes index

开发者 https://www.devze.com 2023-04-12 09:40 出处:网络
for example,I want to get the value of instance of NSMutableData by an index,how should I do?I do it like this:

for example,I want to get the value of instance of NSMutableData by an index,how should I do?I do it like this:

Byte *tempByte = (Byte*)malloc(1); 
memcpy(tempByte, [[nsmutabledata subdataWithRange:NSMakeRange(5, 5)] bytes], 1) ;

but I think it is not a good method. update: I have solved the problem.thank开发者_开发百科 you...


NSData *data = [NSData dataWithContentsOfFile:filePath];
NSUInteger len = [data length];
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [data bytes], len);

This code will dynamically allocate the array to the correct size (you must free(byteData) when you're done) and copy the bytes into it.

You could also use getBytes:len: as indicated by others if you want to use a fixed length array. This avoids malloc/free but is less extensible and more prone to buffer overflow issues so I rarely ever use it.

0

精彩评论

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