【问题标题】:How to convert HEX to NSString in Objective-C?如何在 Objective-C 中将 HEX 转换为 NSString?
【发布时间】:2011-09-19 06:15:46
【问题描述】:

我有一个带有十六进制字符串的 NSString,例如“68656C6C6F”,意思是“你好”。

现在我想将十六进制字符串转换为另一个显示“hello”的 NSString 对象。该怎么做?

【问题讨论】:

  • 澄清一下,十六进制字符串是 NSString 还是整数?我认为您的一些回答者可能对此感到困惑。
  • 十六进制字符串是一个NSString,我想把它转换成另一个代表“hello”的NSString
  • 好吧,用这些信息更新你的问题,这样其他人就不会再犯同样的错误了。
  • 对我的问题有什么建设性的建议吗?
  • @user403015 我们为您提供答案;)两者都按描述工作......

标签: iphone ios ipad


【解决方案1】:
+(NSString*)intToHexString:(NSInteger)value
{
return [[NSString alloc] initWithFormat:@"%lX", value];
}

【讨论】:

  • 我应该在参数值中输入“68656C6C6F”吗?另外我认为您的解决方案不正确。
  • 不仅不正确,而且还违反了所有权规则。愉快的调试/泄漏修复。
  • 我认为这是最好的答案,我在答案中添加了更多示例代码
  • 这个答案应该被删除。问题是关于将包含十六进制数据的字符串转换为包含十六进制数据所代表的文本的字符串。
【解决方案2】:

我确信有更好、更聪明的方法可以做到这一点,但这个解决方案确实有效。

NSString * str = @"68656C6C6F";
NSMutableString * newString = [[[NSMutableString alloc] init] autorelease];
int i = 0;
while (i < [str length])
{
    NSString * hexChar = [str substringWithRange: NSMakeRange(i, 2)];
    int value = 0;
    sscanf([hexChar cStringUsingEncoding:NSASCIIStringEncoding], "%x", &value);
    [newString appendFormat:@"%c", (char)value];
    i+=2;
}

【讨论】:

    【解决方案3】:

    应该这样做:

    - (NSString *)stringFromHexString:(NSString *)hexString {
    
        // The hex codes should all be two characters.
        if (([hexString length] % 2) != 0)
            return nil;
    
        NSMutableString *string = [NSMutableString string];
    
        for (NSInteger i = 0; i < [hexString length]; i += 2) {
    
            NSString *hex = [hexString substringWithRange:NSMakeRange(i, 2)];
            NSInteger decimalValue = 0;
            sscanf([hex UTF8String], "%x", &decimalValue);
            [string appendFormat:@"%c", decimalValue];
        }
    
        return string;
    }
    

    【讨论】:

    • [hex UTF8String] 应该是 [hex cStringUsingEncoding:NSASCIIStringEncoding] 如果它是十六进制它是 ASCII
    【解决方案4】:

    我认为建议 initWithFormat 的人是最好的答案,因为它是 Objective-C 而不是 ObjC、C 的混合......(虽然示例代码有点简洁)......我做了以下

    unsigned int resInit = 0x1013;
    if (0 != resInit)
    {
        NSString *s = [[NSString alloc] initWithFormat:@"Error code 0x%lX", resInit];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Initialised failed"
            message:s
            delegate:nil
            cancelButtonTitle:@"OK"
            otherButtonTitles:nil];
        [alert show];
        [alert release];
        [s release];
    }
    

    【讨论】:

      【解决方案5】:
      extension String {
          func hexToString()-> String {
              var newString = ""
              var i = 0
              while i < self.count {
                  let hexChar = (self as NSString).substring(with: NSRange(location: i, length: 2))
                  if let byte = Int8(hexChar, radix: 16) {
                      if (byte != 0) {
                          newString += String(format: "%c", byte)
                      }
                  }
                  i += 2
              }
              return newString
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-30
        • 2018-04-27
        • 2018-04-06
        • 2014-10-25
        • 2016-10-22
        • 2011-09-18
        • 2020-10-26
        • 2016-12-17
        相关资源
        最近更新 更多