12,554,005 members (66,955 online)
alternative version

42K views
17 bookmarked
Posted

# Computer Conversions (Hexadecimal, Decimal, Octal and Binary)

, 5 Feb 2007 CPOL
 Rate this:
An article to demonstrate how to convert from one base 2, 8, 10 or 16 to another of base 2, 8, 10, 16

## 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() )
{
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() )
{
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

## Share

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

## You may also be interested in...

 Pro Pro

 First Prev Next
 Windows calculator Mark Nischalke5-Feb-07 17:38 Mark Nischalke 5-Feb-07 17:38
 Re: Windows calculator Patrick Sears5-Feb-07 20:38 Patrick Sears 5-Feb-07 20:38
 Re: Windows calculator Mark Nischalke6-Feb-07 1:34 Mark Nischalke 6-Feb-07 1:34
 Re: Windows calculator Vasudevan Deepak Kumar31-Oct-07 20:49 Vasudevan Deepak Kumar 31-Oct-07 20:49
 Enhancing it further, as a full-fledged demo application would be greatly helpful for beginners as well. From within a C# Application, I don't think you can consume Windows Calculator services. Isn't it? Vasudevan Deepak Kumar Personal Homepage Tech Gossips
 Grumpy are we? Shawn Poulson6-Feb-07 3:41 Shawn Poulson 6-Feb-07 3:41
 Re: Grumpy are we? alrsds21-Sep-09 17:26 alrsds 21-Sep-09 17:26
 Last Visit: 31-Dec-99 18:00     Last Update: 25-Oct-16 3:44 Refresh 1