按长度检索 NSData 到十六进制
我得到了一个 NSData,其中包含类似 <00350029 0033> 的字节长度为 6,是否有任何正确的方法可以将字节分割为数组,例如 (00, 35, 00, 29, 00, 33) ?
I got a NSData that contain bytes like <00350029 0033> with length 6, is there any correct way to split the bytes to array somehow like (00, 35, 00, 29, 00, 33) ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
(假设您希望字节作为十六进制字符串表示形式,如示例中所示。否则,请使用
NSNumber
。)(Assuming you want the bytes as a hex string representation, as in your example. Otherwise, use
NSNumber
.)您可以使用 NSData 方法
获取给定范围内的字节(在使用 malloc 分配正确数量的内存之后),然后用于
创建新的小(1 字节长)数据对象,然后将其放入数组中。但是,如果您只是检索指向字节本身的指针(使用 [data bytes]),则会为您提供一个指针(一种 C 意义上的数组,而不是 NSArray,但也可以使用并且效率更高)。
You could use the NSData method
to get the bytes in a given range (after having allocated the right amount of memory, using malloc), then use
to create new small (1 byte long) data objects which you then put into an array. However if you just retrieve the pointer to the bytes themselves (using [data bytes]), that gives you a pointer (kind of an array in the C sense, not an NSArray, but could also be used and far more efficient).