How to convert NSData to multiple type Ints

99封情书 提交于 2019-12-10 18:35:54

问题


I obtain magnetometer trim register as get NSData() that looks as follows:

<00001a1a 4f56f202 00000000 1dfd421b>

I need to convert it to Int8, UInt8, Int16, UInt16 depending on which byte I access.

Sources from docs:

s8 dig_x1;/**< trim x1 data */
s8 dig_y1;/**< trim y1 data */

s8 dig_x2;/**< trim x2 data */
s8 dig_y2;/**< trim y2 data */

u16 dig_z1;/**< trim z1 data */
s16 dig_z2;/**< trim z2 data */
s16 dig_z3;/**< trim z3 data */
s16 dig_z4;/**< trim z4 data */

u8 dig_xy1;/**< trim xy1 data */
s8 dig_xy2;/**< trim xy2 data */

u16 dig_xyz1;/**< trim xyz1 data *

The main problem is how to access a selected byte in NSData to convert it manually either to Int8 or UIint16 etc?

Generally, how to approach such problem? Should look for a way to manually iterate over NSData and convert each value manualy as well?


回答1:


You can convert data.bytes + offset to a pointer of the appropriate type and then dereference the pointer:

let dig_x1 = UnsafePointer<Int8>(data.bytes).memory
let dig_y1 = UnsafePointer<Int8>(data.bytes + 1).memory
// ...
let dig_z1 = UnsafePointer<UInt16>(data.bytes + 4).memory
let dig_z2 = UnsafePointer<Int16>(data.bytes + 6).memory
// ...

(Note: Here it is assumed that all values in that binary blob are property aligned for their type.)

The data is in little-endian byte order, which is also what all current iOS platforms use. To be on the safe side, convert the data to host byte order explicitly:

let dig_z1 = UInt16(littleEndian: UnsafePointer(data.bytes + 4).memory)
let dig_z2 = Int16(littleEndian: UnsafePointer(data.bytes + 6).memory)
// ...

An alternative is to define a C structure in the bridging header file

struct MagnetometerData {
    int8_t dig_x1;
    int8_t dig_y1;

    int8_t dig_x2;
    int8_t dig_y2;

    uint16_t dig_z1;
    int16_t dig_z2;
    int16_t dig_z3;
    int16_t dig_z4;

    uint8_t dig_xy1;
    int8_t dig_xy2;

    uint16_t dig_xyz1;
} ;

and extract the data in one step:

var mdata = MagnetometerData()
data.getBytes(&mdata, length: sizeofValue(mdata))

This works (if there is no padding between the struct members) because Swift preserves the layout of structures imported from C.


A possible Swift 3 implementation of the first approach is

let dig_x1 = ((data as NSData).bytes).load(as: Int8.self) 
let dig_y1 = ((data as NSData).bytes + 1).load(as: Int8.self)
// ...
let dig_z1 = ((data as NSData).bytes + 4).load(as: UInt16.self) 
let dig_z2 = ((data as NSData).bytes + 6).load(as: Int16.self) 
// ...

Again it is assumed that all values are property aligned for their type.



来源:https://stackoverflow.com/questions/39390211/how-to-convert-nsdata-to-multiple-type-ints

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