1

您好,我是 iOS 的初学者在我的一项活动中,我有 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 。请帮助我....提前致谢

4

2 回答 2

1

我试过这样: -

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

于 2013-10-03T06:26:56.207 回答
0

这个问题的一个可能的解决方案:

+(NSString*)hexFromStr:(NSString*)str
{
NSData* nsData = [str dataUsingEncoding:NSUTF8StringEncoding];
const char* data = [nsData bytes];
NSUInteger len = nsData.length;
NSMutableString* hex = [NSMutableString string];
for(int i = 0; i < len; ++i)[hex appendFormat:@"%02X", data[i]];
return hex;
}
于 2013-10-03T06:27:35.933 回答