Convert a string like "ff0000" to a hexadecimal number in object c

I need to convert string input from a UI to a hexadecimal number in an object c. The string is an HTML color code, such as "ff0000" ("#" will be seen with the chart), and it is assumed that it will be converted to ff0000 to provide a color choice. Alternatively, is there a color choice in the c? did not find one in xib objects.

+4
source share
2 answers

From Objective-C parse a hexadecimal string to an integer

unsigned result = 0; NSScanner *scanner = [NSScanner scannerWithString:@"ff0000"]; [scanner scanHexInt:&result]; 

or using "c":

 uint32_t value; NSString *stringValue = @"ff0000"; const char *string = [stringValue cStringUsingEncoding:NSUTF8StringEncoding]; sscanf(string, "%x" , &value); 
+13
source

You can use the following line to convert. Its only one line code:

 NSString *hexString = @"01FFFFAB"; length = (UInt64)strtoull([sizeLine UTF8String], NULL, 16); NSLog(@"The required Length is %d", length); 

Happy coding !!!

+1
source

All Articles