簡體   English   中英

如何將NSString轉換為NSInteger的十六進制表示

[英]How can I cast an NSString as a hex representation of NSInteger

演員不是正確的詞,但請忍受我。 我有以下字符串:

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最合適的方法是什么?

原來這很簡單:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM