How to convert NSData to Array of UInt8 ins iOS Objective C?

痴心易碎 提交于 2019-12-12 04:06:18

问题


I have Swift code for convert this method:

func DATA_TO_UINT8(_ d:Data) -> Array<UInt8> {
        return d.withUnsafeBytes {
            [UInt8](UnsafeBufferPointer(start: $0, count: (d.count)))
        }
}  

and now i want it in Objective C.


回答1:


To produce a stack based array try something like (code typed directly into answer, expect errors):

NSData *data = ...

UInt8 buf[data.length]; // local stack array
[data getBytes:buf length:data.length];

If you want a heap array you will need to use malloc to allocate the memory, if you don't actually need the array just a C pointer you can use the NSData bytes method.



来源:https://stackoverflow.com/questions/43907919/how-to-convert-nsdata-to-array-of-uint8-ins-ios-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!