你好我是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 as 772589fb51d3。请帮帮我.... 在此先感谢
答案 0 :(得分: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
答案 1 :(得分: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;
}