IOS:Convert hex values from the characterstic.value result

扶醉桌前 提交于 2019-12-23 04:47:12

问题


i am able to retrieve value from the peripheral as hex value and i need to convert as per my requirement.[24/12/14 11:37:00 am] sonali_phatak: I can see that i have received proper response.from 01117100352e36302e313100000000e55a

   01 - 01-start byte
   11 - 17(Dec) - length of responce packet
   71 - response ID
   00 - Ignore this byte

    So now out of total length 17, first 4 bytes are header, last 2 bytes are CRC. We   
  need to read remaining 11 bytes and convert them to ASCII.
  35 - 5
  2e - .
  36 - 6
  30 - 0
  2e - .
  31 - 1
  31 - 1
  So Iam getting version number from watch as 5.60.11

But i need to show the above value 5.60.11 in string and print in console . how to convert it pleas help me


回答1:


Please try this :

NSString *strOriginalHex= @"01117100352e36302e313100000000e55a";
NSString *strNewHexForVersion = [strOriginalHex substringWithRange:NSMakeRange(8, 14)];
NSLog(@"%@",[self stringFromHexString:strNewHexForVersion]);//5.60.11


- (NSString *)stringFromHexString:(NSString *)aStrHexString
{
    // The hex codes should all be two characters.
    if (([aStrHexString length] % 2) != 0)
        return nil;

    NSMutableString *aMutStrNewString = [NSMutableString string];
    for (NSInteger i = 0; i < [aStrHexString length]; i += 2)
    {
        NSString *hex = [aStrHexString substringWithRange:NSMakeRange(i, 2)];
        NSInteger decimalValue = 0;
        sscanf([hex UTF8String], "%x", &decimalValue);
        [aMutStrNewString appendFormat:@"%c", decimalValue];
    }

    return aMutStrNewString;
}


来源:https://stackoverflow.com/questions/27645237/iosconvert-hex-values-from-the-characterstic-value-result

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