Convert uint8_t and uint16_t to NSMutableData

I am new to programming and I am trying to develop a Bluetooth mobile device using CoreBluetooth for iOS. I bought several different devices from different manufacturers of testing equipment and one of the instructions for sending values ​​to the device

I want to be able to write a value (fitness goal) for a Bluetooth fitness device. I know how to read / write using bluetooth.

I just don't know how to convert the values ​​I want to use uint8_t and uint16_t, and then combine them into an 8-byte NSMutableData.

Can someone tell me how I will do this?

+4
source share
1 answer

, , - . uint8_t , - . big-endian little-endian. .

:

uint8_t days = 12;
uint16_t walked = 225;
uint16_t ran = 110
uint16_t steps = 750;
uint8_t users = 2;

NSMutableData *data = [NSMutableData data];
[data appendBytes:&days length:1];
uint16_t network = CFSwapInt16HostToBig(walked);
[data appendBytes:&network length:2];
network = CFSwapInt16HostToBig(ran);
[data appendBytes:&network length:2];
network = CFSwapInt16HostToBig(steps);
[data appendBytes:&network length:2];
[data appendBytes:&users length:1];

, big-endian. , CFSwapInt16HostToBig CFSwapInt16HostToLittle.

+8

Source: https://habr.com/ru/post/1548569/


All Articles