【问题标题】:How can I cast an NSString as a hex representation of NSInteger如何将 NSString 转换为 NSInteger 的十六进制表示
【发布时间】:2015-03-20 10:24:19
【问题描述】:

演员不是正确的词,但请耐心等待。我有以下字符串:

NSString *string = @"01700000";

但是,我需要检查它是否包含我正在寻找的特定位:

if (bitshift & (1 << 21)) {
    NSLog(@"YES");
} else {
    NSLog(@"NO");
}

使用[string integerValue] 获取整数值会产生意想不到的结果:

NSInteger bitshift = (1 << 24) | (1 << 22) | (1 << 21) | (1 << 20);
NSString *flags = @"01700000";
NSInteger bitshiftString = [flags integerValue];

// bitshift: 1700000, bitshiftString: 19f0a0
NSLog(@"bitshift: %tx, bitshiftString: %tx", bitshift, bitshiftString);

将此字符串值扫描成NSInteger 的最合适方法是什么?

【问题讨论】:

    标签: objective-c nsstring nsinteger


    【解决方案1】:

    事实证明这非常简单:

    unsigned int bitshiftString;
    [[NSScanner scannerWithString:flags] scanHexInt:&bitshiftString];
    

    【讨论】:

      猜你喜欢
      • 2012-06-16
      • 2011-05-15
      • 2017-04-03
      • 2011-03-04
      • 1970-01-01
      • 2011-09-04
      • 2011-11-23
      相关资源
      最近更新 更多