【问题标题】:how to convert Nsstring value into Hexdecimal in ios [duplicate]如何在ios中将Nsstring值转换为十六进制[重复]
【发布时间】:2013-10-03 06:13:06
【问题描述】:

您好,我是 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 .请帮助我.... 提前致谢

【问题讨论】:

  • [请通过以下链接可能会对您有所帮助][1] [1]:stackoverflow.com/questions/9888456/ios-convert-hex-value
  • 我想把nsstring转成十六进制格式
  • 这个问题已经被问过很多次了。在发布您的问题之前,您是否尝试过查看 SO?
  • 我已经检查过了......如果你有任何答案,那么请不要浪费时间

标签: iphone ios objective-c


【解决方案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

【讨论】:

  • 这只是你的要求吗?
【解决方案2】:

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

+(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;
}

【讨论】:

    猜你喜欢
    • 2017-04-03
    • 2011-03-04
    • 1970-01-01
    • 2014-02-24
    • 2012-06-16
    • 2011-05-15
    • 2013-06-16
    • 2013-05-31
    相关资源
    最近更新 更多