Click here to Skip to main content
15,886,518 members
Articles / Programming Languages / C#
Article

Hex Converter

Rate me:
Please Sign up or sign in to vote.
4.38/5 (22 votes)
18 Jun 2003 259.6K   3.8K   33   17
This article is about a program which helps you convert numeric value from decimal to hexadecimal representation and vice versa.

Introduction

This article is about how you would go about converting numeric values to hexadecimal and vice versa. The main reason why I did this program is to help me to cheat when playing computer games, mainly role-playing ones cos I normally need to find the correct hex data in those save game file and furthermore I do not have a good hex editor to help me do those conversion. :)

Using the code

The main code used for the conversion are from the System.Convert class. Below is the code that is used to convert a string to unsigned integer while checking for overflow, and then convert it to hexadecimal format before showing it as a string in a text box.

C#
// To hold our converted unsigned integer32 value
uint uiDecimal = 0;

try
{
    // Convert text string to unsigned integer
    uiDecimal = checked((uint)System.Convert.ToUInt32(tbDecimal.Text));
}

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

// Format unsigned integer value to hex and show in another textbox
tbHex.Text = String.Format("{0:x2}", uiDecimal);

and vice versa

// To hold our converted unsigned integer32 value
uint uiHex = 0;

try
{
    // Convert hex string to unsigned integer
    uiHex = System.Convert.ToUInt32(tbHex.Text, 16);
}

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

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

Conclusion

This is my first attempt at C# and WinForms so I believe by starting with such simple and small programs, it will eventually build up my skills and knowledge to attempt bigger ones. :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Singapore Singapore
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionRFID Chip Read can not more 10 digit in the TextBox! Pin
tsmeier30-Jul-12 22:37
tsmeier30-Jul-12 22:37 
GeneralGood Work !!! Pin
Ashutosh Phoujdar28-Jan-09 21:15
Ashutosh Phoujdar28-Jan-09 21:15 
Generali'll write an article too!! Pin
Seishin#7-Apr-08 23:46
Seishin#7-Apr-08 23:46 
GeneralCool!! Pin
Muammar©8-Apr-07 22:22
Muammar©8-Apr-07 22:22 
GeneralThanks Pin
Laurent Cozic10-Oct-06 8:11
Laurent Cozic10-Oct-06 8:11 
GeneralFile to Hex Pin
chirag parikh4-May-06 20:21
chirag parikh4-May-06 20:21 
GeneralGood Work Pin
Saqib Mehmood8-Aug-05 2:28
Saqib Mehmood8-Aug-05 2:28 
GeneralHex To Text Pin
Member 172622411-Feb-05 14:33
Member 172622411-Feb-05 14:33 
GeneralRe: Hex To Text Pin
Weiye Chen11-Feb-05 16:13
Weiye Chen11-Feb-05 16:13 
GeneralCould do this with Windows Calculator. Pin
red_tomato14-May-04 8:12
red_tomato14-May-04 8:12 
GeneralRe: Could do this with Windows Calculator. Pin
Weiye Chen14-May-04 16:41
Weiye Chen14-May-04 16:41 
GeneralSuggestion Pin
leppie19-Jun-03 8:11
leppie19-Jun-03 8:11 
Its normally best to show hex numbers with either 0x notation or in byte groups in either little or big endian formation.

return String.Format("0x{0}", String.Format("{0:X}", num).PadLeft(8,' '));
or

use the bit convertor class to convert the number to a byte[] and print them individually.

Smile | :)

<a TITLE="See my user info" href=http://www.codeproject.com/script/profile/whos_who.asp?id=38829>leppie<a>::<a TITLE="Go to all articles  written by me" href=http://www.codeproject.com/script/articles/list_articles.asp?userid=38829>AllocCPArticle</a>(<a TITLE="Go to my latest, greatest article!" href=http://www.codeproject.com/useritems/dfamachine.asp >Generic DFA State Machine for .NET</a>);

GeneralRe: Suggestion Pin
Weiye Chen19-Jun-03 16:44
Weiye Chen19-Jun-03 16:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.