Convert RGB To Integer In C#

If you ever wanted to convert an RGB value to an integer value, this is how you do it.
First strip the leading pound sign(#) from the string using the Substring function.

   string val = "#060810";
   var    rgb = val.Substring(1, val.Length -1);

Next split the string into 3 different parts representing red, green, and blue

   var r = int.Parse(rgb.Substring(1, 2), NumberStyles.HexNumber);
   var g = int.Parse(rgb.Substring(3, 2), NumberStyles.HexNumber);
   var b = int.Parse(rgb.Substring(5, 2), NumberStyles.HexNumber);

*NB: The int.Parse function along with NumberStyle.HexNumber is used to convert each Hex number to its equivalent decimal representation, happy coding 🙂


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s