Click here to Skip to main content
Licence CPOL
First Posted 5 Feb 2007
Views 29,407
Downloads 779
Bookmarked 16 times

Computer Conversions (Hexadecimal, Decimal, Octal and Binary)

By | 5 Feb 2007 | Article
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

Introduction

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).

Background

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);
        break;
    case "DECIMAL":
        currInput = Convert.ToUInt64(txtObject, 10);
        break;
    case "OCTAL":
        currInput = Convert.ToUInt64(txtObject, 8);
        break;
    case "BINARY":
        currInput = Convert.ToUInt64(txtObject, 2);
        break;
}

The Binary2Output() function contains this code:

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

History

  • 5th February, 2007: First version

License

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

Member

Scott Klawitter programs mainly in Visual Studio .NET with a focus in C# development.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralWindows calculator PinmvpMark Nischalke17:38 5 Feb '07  
GeneralRe: Windows calculator PinmemberPatrick Sears20:38 5 Feb '07  
GeneralRe: Windows calculator PinmvpMark Nischalke1:34 6 Feb '07  
GeneralRe: Windows calculator PinmemberVasudevan Deepak Kumar20:49 31 Oct '07  
GeneralGrumpy are we? PinmemberShawn Poulson3:41 6 Feb '07  
GeneralRe: Grumpy are we? Pinmemberalrsds17:26 21 Sep '09  

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

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

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