How to convert an NSString to hex values

The perfect and short way to convert nsstring to hexadecimal values

NSMutableString *tempHex=[[NSMutableString alloc] init];

[tempHex appendString:@"0xD2D2D2"];

unsigned colorInt = 0;

[[NSScanner scannerWithString:tempHex] scanHexInt:&colorInt];

lblAttString.backgroundColor=UIColorFromRGB(colorInt);

The macro used for this code is—-

#define UIColorFromRGB(rgbValue) 
[UIColor \colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

Leave a Comment