Verify if String is hexadecimal

Horrible abuse of exceptions. Don’t ever do this! (It’s not me, it’s Josh Bloch’s Effective Java). Anyway, I suggest private static final Pattern HEXADECIMAL_PATTERN = Pattern.compile(“\\p{XDigit}+”); private boolean isHexadecimal(String input) { final Matcher matcher = HEXADECIMAL_PATTERN.matcher(input); return matcher.matches(); }

how to set hex color code for background [duplicate]

I like to use this little piece of code to use HTML web colors in my apps. Usage: [self.view setBackgroundColor: [self colorWithHexString:@”FFFFFF”]]; /* white */ The Code: -(UIColor*)colorWithHexString:(NSString*)hex { NSString *cString = [[hex stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString]; // String should be 6 or 8 characters if ([cString length] < 6) return [UIColor grayColor]; // strip 0X … Read more

Python Hexadecimal

Use the format() function with a ’02x’ format. >>> format(255, ’02x’) ‘ff’ >>> format(2, ’02x’) ’02’ The 02 part tells format() to use at least 2 digits and to use zeros to pad it to length, x means lower-case hexadecimal. The Format Specification Mini Language also gives you X for uppercase hex output, and you … Read more

printf() formatting for hexadecimal

The # part gives you a 0x in the output string. The 0 and the x count against your “8” characters listed in the 08 part. You need to ask for 10 characters if you want it to be the same. int i = 7; printf(“%#010x\n”, i); // gives 0x00000007 printf(“0x%08x\n”, i); // gives 0x00000007 … Read more