如何将NSData转换为iOS Objective C中的UInt8数组? [英] How to convert NSData to Array of UInt8 ins iOS Objective C?

查看:1170
本文介绍了如何将NSData转换为iOS Objective C中的UInt8数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 Swift 代码用于转换此方法:

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)))
        }
}  

现在我要在 Objective C 中使用它.

推荐答案

要生成基于堆栈的数组,请尝试类似的操作(将代码直接键入到答案中,以免出现错误):

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];

如果要使用堆数组,则需要使用malloc来分配内存,如果实际上不需要数组只是C指针,则可以使用NSData bytes方法.

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.

这篇关于如何将NSData转换为iOS Objective C中的UInt8数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆