Today I needed to parse colors encoded as string in the form 0xRRGGBB where RR GG and BB were red green and blue values of given color encoded in hexadecimal.
Problem I stumbled upon, was, what if I have a number like:
There was no problem converting it to System.Drawing.Color class, but back to string.
I used code like below:
This would yield:
Which is definitely not what I wanted it to be. I needed a way to enforce a number to emit zero in front of it, if it’s small enough to fit in one digit.
After much too long search, trial and error I found a solution, that was so obvious when I finally discovered it, that I felt like it should be the first thing to try: you just give a number right after ‘X’, indicating how many chars you want the number to have. Considering the fact that I tried ‘X,2’, ‘X:2’, ‘X;2’ ‘X,00’ and several more before I tried this I feel now… well, not very well about myself 😉
Took me like 30 minutes to figure it out.