Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Converting RGB to hex Numeric Value

Here's a snippet of code I'm having trouble with... It generates random R, G and B components separately based on 0-255, but issue is whenever one (or more) of the parts generate within the first 16 values (0-15). That value will only display one of the two places within the hex string.

For example, if green generated a value of 9, the hex string would look like "A7 9B3"... missing the 3rd place value. It looks like the color outputs correctly.. it's just the string that is not displaying correctly.

I'm sure it's something simple and I've tried to figure it out, but I don't think I know what I should be looking for exactly.

Any help would be appreciated.

rgbRedValue = CGFloat(arc4random_uniform(256))
rgbGreenValue = CGFloat(arc4random_uniform(256))
rgbBlueValue = CGFloat(arc4random_uniform(256))

hexValue = String(format:"%2X", Int(rgbRedValue)) + String(format:"%2X", Int(rgbGreenValue)) + String(format:"%2X", Int(rgbBlueValue))

2 Answers

ah, I see now. you can just specify to have a minimum width of 2 and pad it with zeroes using %02X:

let rgbRedValue = 200
let rgbGreenValue = 13
let rgbBlueValue = 45

let hexValue = String(format:"%02X", Int(rgbRedValue)) + String(format:"%02X", Int(rgbGreenValue)) + String(format:"%02X", Int(rgbBlueValue))

Ahhh, you're awesome. Thank you.

I'm pretty new to Obj-C/Swift, so I have been wracking my brain trying to figure this out. I will have to go look up "padding" now so I have that in the vault. I actually tried putting the 0 before the % sign, but didn't think to try it after. Much appreciated!

looking at the String Format Specifiers, the hexadecimal format specifier is %X for capital letters. have you tried just %X instead of %2X.

hexValue = String(format:"%X", Int(rgbRedValue)) + String(format:"%X", Int(rgbGreenValue)) + String(format:"%X", Int(rgbBlueValue))

I just tried the above in playgrounds and it seemed to work fine, whereas with %2X i did notice the empty space.

Stone, thanks for answering. Let me clarify the question a bit. Your answer does remove the empty space, but I don't want to remove it, I want to fill it in. I guess by default, the compiler is dropping the leading "0" in the first 16 possible values (0-15). I want to somehow retain that leading "0" so the hexadecimal remains 6 place values.

Example of the problem...

  1. Generate random red, green and blue color components = 200, 13, 45
  2. Hex equivalent = C80D2D
  3. Compiles as = C8 D2D (omitting the leading zero of the green value
  4. I assign the hex value to a label and it shows a missing value

I hope that better explains what I'm asking. Thanks.