Click here to Skip to main content
11,927,167 members (58,591 online)
Click here to Skip to main content
Add your own
alternative version


17 bookmarked

Computer Conversions (Hexadecimal, Decimal, Octal and Binary)

, 5 Feb 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
An article to demonstrate how to convert from one base 2, 8, 10 or 16 to another of base 2, 8, 10, 16
Sample image


This code was written to show how to convert from Hexadecimal, Decimal, Octal or Binary out to a Hexadecimal, Decimal, Octal or Binary value. Many functions demonstrate Hex2Bin, Bin2Hex, Hex2Dec, Dec2Hex, etc. This code will show you how easy it is to convert between any of these bases (2, 8, 10, 16).


It is good to understand how binary conversions work. Take a look here for more information.

Using the Code

Enter an input value, and see what the output value is. It's that easy. Use the arrows to add or subtract the value.

The two main functions that deal with the conversions include Input2Binary() and Binary2Output(). From these functions, you can take any input type and convert it to binary, and then convert that binary value to any output type.

The Input2Binary() function contains this code:

// Convert the current input into binary 
// NOTE: convert string from base 'n'
switch( numberStyle.ToUpper() )
    case "HEXADECIMAL":
        currInput = Convert.ToUInt64(txtObject, 16);
    case "DECIMAL":
        currInput = Convert.ToUInt64(txtObject, 10);
    case "OCTAL":
        currInput = Convert.ToUInt64(txtObject, 8);
    case "BINARY":
        currInput = Convert.ToUInt64(txtObject, 2);

The Binary2Output() function contains this code:

// Convert the int to the correct output
switch( numberStyle.ToUpper() )
    case "HEXADECIMAL":
        txtObject = Convert.ToString(currVal, 16).ToUpper();
    case "DECIMAL":
        txtObject = Convert.ToString(currVal, 10);
    case "OCTAL":
        txtObject = Convert.ToString(currVal, 8);
    case "BINARY":
        txtObject = Convert.ToString(currVal, 2);


  • 5th February, 2007: First version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Scott Klawitter
Web Developer
United States United States
Scott Klawitter programs mainly in Visual Studio .NET with a focus in C# development.

You may also be interested in...

Comments and Discussions

GeneralWindows calculator Pin
Mark Nischalke5-Feb-07 18:38
mvpMark Nischalke5-Feb-07 18:38 
GeneralRe: Windows calculator Pin
Patrick Sears5-Feb-07 21:38
memberPatrick Sears5-Feb-07 21:38 
GeneralRe: Windows calculator Pin
Mark Nischalke6-Feb-07 2:34
mvpMark Nischalke6-Feb-07 2:34 
GeneralRe: Windows calculator Pin
Vasudevan Deepak Kumar31-Oct-07 21:49
memberVasudevan Deepak Kumar31-Oct-07 21:49 
GeneralGrumpy are we? Pin
Shawn Poulson6-Feb-07 4:41
memberShawn Poulson6-Feb-07 4:41 
GeneralRe: Grumpy are we? Pin
alrsds21-Sep-09 18:26
memberalrsds21-Sep-09 18:26 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 5 Feb 2007
Article Copyright 2007 by Scott Klawitter
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid