Search code examples
objective-cuicoloruint32

Objective C: uint32_t stored as String, need to read as uint32_t


I am storing color values in a database for an iPad app that needs a legend with colors in it. Each entry has it's own color value, derived from a hex value. Basically, my colors all look like this: 0X######. I have functions that can take this value, as a uint32_t, and turn it into the color I need. However, I store the value as a String.

What I need to do is convert this string to a uint32_t. I need "0X######" to equal 0X######, if that makes sense. I know this might not be possible, in which case I'll have to find another solution.


Solution

  • You can use NSScanner for this.

    NSScanner * scanner = [NSScanner scannerWithString:@"0XAABBCC"];
    uint32_t val;
    [scanner scanHexLongLong:&val];