Reputation: 748
How can I convert a integer 6 digit number such as "1234674" to a hexadecimal 4 byte NSData in Objective-C ?
This is the part of our code that sends passkey:
#define HSL_PRIVATE_SERVICE_UUID 0xFF20
#define HSL_PRIVATE_NEW_PASSKEY_UUID 0xFF35
unsigned int newPassKey = [_confirmNewPassKey.text intValue];
NSLog(@"newPasskey %d", newPassKey);
NSData *d = [NSData dataWithBytes:&newPassKey length:sizeof(unsigned int)];
[_t writeValue:HSL_PRIVATE_SERVICE_UUID characteristicUUID:HSL_PRIVATE_NEW_PASSKEY_UUID p:_peripheral data:d];
I did an air capture comparing BTOOL versus iPhone passkey writes.
BTOOL (A simulator tool) wrote (the correct result) :
0x40e201
iPhone wrote(wrong data):
0x0001e240
Not sure what is going on and how to fix it in the app so that the result matches what the Bluetooth device is expecting . I would like the result to be same as BTOOL one.
Upvotes: 0
Views: 1509
Reputation: 318804
Try this:
uint32_t value = [_confirmNewPassKey.text intValue];
uint32_t swapped = CFSwapInt32HostToBig(value);
NSData *d = [NSData dataWithBytes:&swapped length:sizeof(swapped)];
This assumes you want big endian for the output.
Upvotes: 1