Convert Int value to Hex value and vice versa in C#

by Emanuele 12/9/2007 5:00:00 PM

This morning I created another function to convert something in other something.
This time this function convert Int Value to Hex Value and Hex Value to Int Value.
You find a demo at this link.

    protected void CmdConvert1(object sender, EventArgs e)
    {
        //set the default value
        uint uiDecimal = 0;

        try
        {
            // Convert text string to integer
            uiDecimal = Convert.ToUInt32(TxtText1.Text);
        }

        catch (OverflowException exception)
        {
            // Show overflow message and return
            TxtHex1.Text = "Overflow";
            return;
        }

        // Format hex value and show in another textbox
        TxtHex1.Text = String.Format("{0:x2}", uiDecimal);
    }
   
    protected void CmdConvert2(object sender, EventArgs e)
    {
        uint uiHex = 0;

        try
        {
            // Convert hex value to int
            uiHex = Convert.ToUInt32(TxtHex2.Text, 16);
        }

        catch (OverflowException exception)
        {
            // Show overflow message and return
            TxtText2.Text = "Overflow";
            return;
        }

        // Format it and show as a string
        TxtText2.Text = uiHex.ToString();
    }

Related posts



Powered by BlogEngine.NET 1.3.1.0
Theme by Emanuele Bartolesi

About the author

Name of author Emanuele Bartolesi
I'm a senior developer and project manager.

Contact me Contact me

Calendar

<<  December 2008  >>
MoTuWeThFrSaSu
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in

Download Day 2008