IOS:从characterstic.value结果转换十六进制值 [英] IOS:Convert hex values from the characterstic.value result

查看:206
本文介绍了IOS:从characterstic.value结果转换十六进制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够从外设中检索值作为十六进制值,并且需要按照我的要求进行转换。[24/12/14 11:37:00 am] sonali_phatak:我可以看到我收到了正确的回应。从01117100352e36302e313100000000e55a

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

但是我需要在字符串中显示上面的值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

推荐答案

请试试这个:

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;
}

这篇关于IOS:从characterstic.value结果转换十六进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆