This question already has answers here:
How to convert an NSString to hex values

(8个答案)


6年前关闭。




您好,我是iOS的初学者。在我的一项 Activity 中,我有NSString并想转换为十六进制格式
    NSString  *str= 131003112444371;

long long decimalRepresentation = 131003112444371;
    NSLog(@"date %llx",decimalRepresentation);

    NSLog(@"Hex value of char is 0x%02llx",decimalRepresentation);

我正在使用它然后得到结果0x772589fb51d3,我想提取此否772589fb51d3
当我为此使用这些行时..
      NSString *str1=[NSString stringWithFormat:@"%llu",decimalRepresentation];

      NSLog(@"str %@",str1);


    NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"0x"];
    str1 = [[str1 componentsSeparatedByCharactersInSet:doNotWant]componentsJoinedByString:@""];
    NSLog(@"%@", str1); // => foobarbazfoo

但是这行再次将十六进制值转换为字符串,我不提取该值
0x772589fb51d3为772589fb51d3。请帮助我...。
提前致谢

最佳答案

我已经这样尝试过:

NsString  *str= 131003112444371;

long long decimalRepresentation = 131003112444371;
    NSLog(@"date %llx",decimalRepresentation);

    NSLog(@"Hex value of char is 0x%02llx",decimalRepresentation);

//只是将llu替换为llx
   NSString *str1=[NSString stringWithFormat:@"%llx",decimalRepresentation];

      NSLog(@"str %@",str1);

我到这里的输出是 772589fb51d3

关于iphone - 如何在iOS中将Nsstring值转换为十六进制,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19152032/

10-14 23:42