DCDC DCDC - 1 year ago 115
Swift Question

How to convert NSData to multiple type Ints

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 *

enter image description here

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?

Answer Source

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
// ...

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download