IOS:Convert hex values from the characters.value 结果

标签 ios objective-c hex ascii core-bluetooth

我能够从外围设备中检索值作为十六进制值,我需要根据我的要求进行转换。[24/12/14 11:37:00 am] sonali_phatak:我可以看到我收到了正确的回复。来自 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 并在控制台中打印。如何转换请帮助我

最佳答案

请尝试这个:

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:Convert hex values from the characters.value 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27645237/

相关文章:

C#串口读取HEX数据

Golang 十六进制编码

iphone - iOS : date problem

iphone - 读取数组

ios - 警告 : Attempt to present whose view is not in the window hierarchy! swift

javascript - 如何将 javascript 文件加载到 osx 应用程序中的 WebView 中?

objective-c - 指向 Swift 函数中的指针参数的指针

hex - 如何在 Rust 中将十六进制值转换为 Base64

ios - 使用 NSRegularExpression 检测字符串是否包含西里尔字母的示例

ios - Google Cloud Messaging (iOS) 将文件放在默认应用程序文档文件夹中