简体   繁体   中英

NSString literal to uint32_t

I'm not sure if the title is correct for what I need to do, but I simply need to take a literal NSString string and make it a uint32_t

Basically this is what I'm wanting to do:

NSString *literal = @"0x0001234";
uint32_t = literal; // uint32_t = 0x0001234;

I tried using some casts but those just gave me errors, so how can I do this?

*I'm not too familiar with Obj-C, so not sure what I should be searching for exactly.

You can't use a simple assignment, even with a case, because NSString and uint32 are completely different things.

You can use the NSScanner scanHexInt method. Note that this method can fail if the string isn't a valid hex string.

NSString *literal = @"0x0001234";
unsigned output = 0;
if ([[NSScanner scannerWithString:literal] scanHexInt:&output]) {
   // Do something with output
} else {
   // Unable to parse string
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM