简体   繁体   English

将Unsigned Int转换为十六进制值

[英]Convert Unsigned Int to Hexadecimal value

How can I convert a integer 6 digit number such as "1234674" to a hexadecimal 4 byte NSData in Objective-C ? 如何在Objective-C中将6位整数(例如“ 1234674”)转换为16位4字节NSData?

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和iPhone的密钥编写。

BTOOL (A simulator tool) wrote (the correct result) : BTOOL(模拟器工具)写道(正确的结果):

0x40e201 

iPhone wrote(wrong data): iPhone写道(数据错误):

 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. 我希望结果与BTOOL one相同。

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. 假设您要输出大字节序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM