Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Page History: Converting Between Hex and Integer

Compare Page Revisions



« Older Revision - Back to Page History - Current Revision


Page Revision: Fri, Oct 09, 2009, 1:21 PM


The following C# code demonstrates how to convert a hexadecimal color value to its three decimal components. The key to the conversion is the Convert.ToInt32(s, 16) function call. (The second parameter is the base to convert from.)

private void ConvertToRgb()
{
    try
    {
        string hex = "000000" + uxHexTextBox.Text ;
        hex = hex.Substring(hex.Length - 6, 6);
        string result = "RGB(";

        for (int i = 0; i <= 4; i += 2)
        {
            string s = hex.Substring(i, 2);
            result += Convert.ToInt32(s, 16).ToString();
            if (i < 4)
                result += ", ";
        }
        result += ")";
        uxRgbTextBox.Text = result;
        Clipboard.SetText(result);
        uxHexTextBox.SelectAll();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, Application.ProductName);
    }
}

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.