objective-c 如何在 iPhone 中将 NSData 转换为字节数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/724086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert NSData to byte array in iPhone?
提问by Chilly Zhong
I want to convert NSDatato a byte array, so I write the following code:
我想转换NSData为字节数组,所以我写了以下代码:
NSData *data = [NSData dataWithContentsOfFile:filePath];
int len = [data length];
Byte byteData[len];
byteData = [data bytes];
But the last line of code pops up an error saying "incompatible types in assignment". What is the correct way to convert the data to byte array then?
但是最后一行代码弹出一个错误,说“赋值中的类型不兼容”。那么将数据转换为字节数组的正确方法是什么?
回答by Matt Gallagher
You can't declare an array using a variable so Byte byteData[len];won't work. If you want to copy the data from a pointer, you also need to memcpy (which will go through the data pointed to by the pointer and copy each byte up to a specified length).
您不能使用变量声明数组,因此Byte byteData[len];不起作用。如果要从指针复制数据,还需要memcpy(它将遍历指针指向的数据并将每个字节复制到指定长度)。
Try:
尝试:
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.
此代码将动态分配数组到正确的大小(free(byteData)完成后必须这样做)并将字节复制到其中。
You could also use getBytes:length: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.
getBytes:length:如果您想使用固定长度的数组,您也可以按照其他人的指示使用。这避免了 malloc/free 但扩展性较差,更容易出现缓冲区溢出问题,所以我很少使用它。
回答by stevex
You could also just use the bytes where they are, casting them to the type you need.
您也可以只使用它们所在的字节,将它们转换为您需要的类型。
unsigned char *bytePtr = (unsigned char *)[data bytes];
回答by Khulja Sim Sim
Already answered, but to generalize to help other readers:
已经回答,但概括以帮助其他读者:
//Here: NSData * fileData;
uint8_t * bytePtr = (uint8_t * )[fileData bytes];
// Here, For getting individual bytes from fileData, uint8_t is used.
// You may choose any other data type per your need, eg. uint16, int32, char, uchar, ... .
// Make sure, fileData has atleast number of bytes that a single byte chunk would need. eg. for int32, fileData length must be > 4 bytes. Makes sense ?
// Now, if you want to access whole data (fileData) as an array of uint8_t
NSInteger totalData = [fileData length] / sizeof(uint8_t);
for (int i = 0 ; i < totalData; i ++)
{
NSLog(@"data byte chunk : %x", bytePtr[i]);
}
回答by Nicholas Riley
The signature of -[NSData bytes]is - (const void *)bytes. You can't assign a pointer to an array on the stack. If you want to copy the buffer managed by the NSDataobject into the array, use -[NSData getBytes:]. If you want to do it without copying, then don't allocate an array; just declare a pointer variable and let NSDatamanage the memory for you.
的签名-[NSData bytes]是- (const void *)bytes。您不能将指针分配给堆栈上的数组。如果要将NSData对象管理的缓冲区复制到数组中,请使用-[NSData getBytes:]. 如果你想不复制就去做,那就不要分配数组;只需声明一个指针变量并让我们NSData为您管理内存。
回答by Kendall Helmstetter Gelner
That's because the return type for [data bytes] is a void* c-style array, not a Uint8 (which is what Byte is a typedef for).
那是因为 [data bytes] 的返回类型是 void* c 样式数组,而不是 Uint8(这是 Byte 的 typedef)。
The error is because you are trying to set an allocated array when the return is a pointer type, what you are looking for is the getBytes:length: call which would look like:
错误是因为当返回是指针类型时您试图设置分配的数组,您正在寻找的是 getBytes:length: 调用,它看起来像:
[data getBytes:&byteData length:len];
Which fills the array you have allocated with data from the NSData object.
它用来自 NSData 对象的数据填充您分配的数组。
回答by jervine10
Here's what I believe is the Swift equivalent:
这是我认为的 Swift 等价物:
if let data = NSData(contentsOfFile: filePath) {
let length = data.length
let byteData = malloc(length)
memcmp(byteData, data.bytes, length)
}

