Convert hex string to long

后端 未结 3 1196
再見小時候
再見小時候 2020-12-09 10:33

Are there any Cocoa classes that will help me convert a hex value in a NSString like 0x12FA to a long or NSNumber? It doesn\'t look l

相关标签:
3条回答
  • 2020-12-09 10:50

    here is the other way conversion, a long long int to hex string.
    first the hex to long long.

    NSString* pString = @"ffffb382ddfe";
    NSScanner* pScanner = [NSScanner scannerWithString: pString];
    
    unsigned long long iValue2;
    [pScanner scanHexLongLong: &iValue2];
    
    NSLog(@"iValue2 = %lld", iValue2);
    

    and the other way, longlong to hex string...

    NSNumber *number;
    NSString *hexString;
    
    number = [NSNumber numberWithLongLong:iValue2];
    hexString = [NSString stringWithFormat:@"%qx", [number longLongValue]];
    
    NSLog(@"hexString = %@", hexString);
    
    0 讨论(0)
  • 2020-12-09 10:51

    Here's a short example of how you would do it using NSScanner:

    NSString* pString = @"0xDEADBABE";
    NSScanner* pScanner = [NSScanner scannerWithString: pString];
    
    unsigned int iValue;
    [pScanner scanHexInt: &iValue];
    
    0 讨论(0)
  • 2020-12-09 10:58

    See NSScanner's scanHex...: methods. That'll get you the primitive that you can wrap in an NSNumber.

    0 讨论(0)
提交回复
热议问题