Convert hex string to long

后端 未结 3 1195
再見小時候
再見小時候 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条回答
  •  萌比男神i
    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);
    

提交回复
热议问题