Click here to Skip to main content
11,928,003 members (25,200 online)
Click here to Skip to main content
Add your own
alternative version


79 bookmarked

Hexadecimal, Binary, and Decimal conversions

, 22 Apr 2005
Rate this:
Please Sign up or sign in to vote.
This project contains a class aimed to all possible combinations for hexadecimal, binary, and decimal conversions.

Sample Image - BinaryAndHexConversions.jpg


By now, you would think that Microsoft would have a set of standard functions available to do Hexadecimal, Binary, and Decimal conversions. I looked around and found the BitArray, but it was not enough for, what I needed here is a set of classes to do all of these conversions including decimal conversions to signed integers and unsigned integers.

The Project

The project consists of a Bits class to do all the bit manipulations, a Hex class to do all the hexadecimal manipulations, a Conversion class that consists of static methods to do miscellaneous set of conversions among hexadecimal, binary, and decimal conversions. The project also includes a HexTextBox class and a HexGrid class.

The HexTextBox class is derived from RichTextBox and its purpose is to ensure that the user enters only valid digits that are within a valid range. The class can be set to Hexadecimal mode, Decimal mode, and Binary mode. When in hexadecimal mode, it prevents user from entering digits that are not hexadecimal. Same functionality for decimal and binary mode. In binary mode, the tooltip is enabled to show the bit position as the mouse is moved across the text box.

The HexGrid class is derived from DataGrid and can be used to edit hexadecimal sets of data.

Conversions Available

public static string IntToBin(int number, int numBits)
public static string UIntToBin(uint number, int numBits)
public static string HexToBin(string hexData)
public static int BinToInt(string binData)
public static uint BinToUInt(string binData)
public static string BinToHex(string binData)
public static int HexToInt(string hexData)
public static uint HexToUInt(string hexData)
public static string IntToHex(int data, int numBytes)
public static string UIntToHex(uint data, int numBytes)
public static int UIntToInt(uint data, int numBits)
public static uint IntToUInt(int data, int numBits)

Additional Conversions

You can add many more conversions as needed. The constructors of the Bits and Hex classes have additional overloads that you can use to create additional conversions.

For example, to convert from an array of bytes to a bit string, you would use the following code:

byte[] byteArray = new byte[] {1,2,3,4,5,6,7,8}; 
string bitString = new Bits(byteArray).ToString();

Additional Documentation

The project includes full documentation with descriptions available for all public methods. The documentation was created with NDoc.

How to Use the Code?

To use the code you just need to include the "Bin" project in your solution, add it to your references and then you should be able to start using any of the conversions available.

For example, to convert from a hexadecimal string to binary string:

string hexData = "AABBCCDD01234567";
string binData = Conversion.HexToBin(hexData);


I hope, you find these classes useful and let me know when you find bugs so that I can correct them in my code.


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


About the Author
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

General.NET ver 2.0 Pin
gordsh19-Jan-06 10:29
membergordsh19-Jan-06 10:29 
GeneralRe: .NET ver 2.0 Pin
rudy.net19-Jan-06 10:41
memberrudy.net19-Jan-06 10:41 
Generalsuggestions Pin
Eric Engler2-May-05 5:38
memberEric Engler2-May-05 5:38 
GeneralMy code Pin
DrGUI29-Apr-05 12:08
memberDrGUI29-Apr-05 12:08 
Your code will be faster for your defined data types, but you can have my code for general conversion up to base 36 or something.
I must have written it about 5 years ago; I was only 10 then so it's in VB6, probably inefficient and poorly written. I'm sorry if it is and you'll be glad that I've moved from VB 6 to VB .NET then to C#.
Do whatever you want with the code below

'Can convert from any base to any other base

Option Explicit

Private Sub cmdCalculate_Click()
Dim aInputNum() As Double
Dim aOutputNum() As Double
Dim i As Long
Dim Base10Num As Variant

On Error GoTo defaulterr

'Convert the input number to base 10

'resize the array holding the numbers of the characters
ReDim aInputNum(Len(InputNum.Text))

'put the characters into an array of characters from a string and converts other symbols
For i = 0 To Len(InputNum.Text) - 1
aInputNum(i) = ConvertIntoNum10(Mid(InputNum.Text, i + 1, 1))
Next i

On Error GoTo base10err

'convert to base 10
For i = 0 To Len(InputNum.Text) - 1
Base10Num = Base10Num + (aInputNum(i) * (FirstBase.Text ^ (Len(InputNum.Text) - i - 1)))
Next i

Erase aInputNum

NumInBase10.Text = Format$(Base10Num, "########################################")

'convert base 10 to the other base

'find the biggest number that fits into Base10Num
Dim MaxTableSize As Long
If Val(SecondBase.Text) ^ MaxTableSize > Base10Num Then Exit Do
MaxTableSize = MaxTableSize + 1

ReDim aOutputNum(MaxTableSize)

Dim TableVal As Variant
Dim TempNum As Double
Dim OutputStr As String
TempNum = Base10Num
For i = 0 To MaxTableSize - 1
TableVal = Val(SecondBase.Text) ^ (MaxTableSize - i - 1)
If Not (TempNum - TableVal) > -1 Then
Exit Do
TempNum = TempNum - TableVal
aOutputNum(i) = aOutputNum(i) + 1
End If
Next i

'make output string
For i = 0 To MaxTableSize - 1
OutputStr = OutputStr & ConvertIntoChar(aOutputNum(i))
Next i

'output, clipping off the leading zeros
OutputNum.Text = Format$(OutputStr, "########################################")

Exit Sub

Debug.Print Err.Description
If Err.Number = 6 Then 'overflow
MsgBox "The number to be converted is too large", vbExclamation, "Number Transformer"
GoTo defaulterr
End If
Exit Sub

MsgBox Err.Number & vbCrLf & Err.Description, vbExclamation, "Number Transformer"
End Sub

Private Sub Form_Load()
#If FDEBUG = 1 Then
MsgBox "using conditional compilation!"
#End If
End Sub

Public Function ConvertIntoNum10(character As String) As Double
character = UCase(character)
Select Case character
Case "0": ConvertIntoNum10 = 0
Case "1": ConvertIntoNum10 = 1
Case "2": ConvertIntoNum10 = 2
Case "3": ConvertIntoNum10 = 3
Case "4": ConvertIntoNum10 = 4
Case "5": ConvertIntoNum10 = 5
Case "6": ConvertIntoNum10 = 6
Case "7": ConvertIntoNum10 = 7
Case "8": ConvertIntoNum10 = 8
Case "9": ConvertIntoNum10 = 9

Case "A": ConvertIntoNum10 = 10
Case "B": ConvertIntoNum10 = 11
Case "C": ConvertIntoNum10 = 12
Case "D": ConvertIntoNum10 = 13
Case "E": ConvertIntoNum10 = 14
Case "F": ConvertIntoNum10 = 15
Case "G": ConvertIntoNum10 = 16
Case "H": ConvertIntoNum10 = 17
Case "I": ConvertIntoNum10 = 18
Case "J": ConvertIntoNum10 = 19
Case "K": ConvertIntoNum10 = 20
Case "L": ConvertIntoNum10 = 21
Case "M": ConvertIntoNum10 = 22
Case "N": ConvertIntoNum10 = 23
Case "O": ConvertIntoNum10 = 24
Case "P": ConvertIntoNum10 = 25
Case "Q": ConvertIntoNum10 = 26
Case "R": ConvertIntoNum10 = 27
Case "S": ConvertIntoNum10 = 28
Case "T": ConvertIntoNum10 = 29
Case "U": ConvertIntoNum10 = 30
Case "V": ConvertIntoNum10 = 31
Case "W": ConvertIntoNum10 = 32
Case "X": ConvertIntoNum10 = 33
Case "Y": ConvertIntoNum10 = 34
Case "Z": ConvertIntoNum10 = 35
End Select
End Function

Public Function ConvertIntoChar(num10 As Double) As String
Select Case num10
Case 0: ConvertIntoChar = "0"
Case 1: ConvertIntoChar = "1"
Case 2: ConvertIntoChar = "2"
Case 3: ConvertIntoChar = "3"
Case 4: ConvertIntoChar = "4"
Case 5: ConvertIntoChar = "5"
Case 6: ConvertIntoChar = "6"
Case 7: ConvertIntoChar = "7"
Case 8: ConvertIntoChar = "8"
Case 9: ConvertIntoChar = "9"

Case 10: ConvertIntoChar = "A"
Case 11: ConvertIntoChar = "B"
Case 12: ConvertIntoChar = "C"
Case 13: ConvertIntoChar = "D"
Case 14: ConvertIntoChar = "E"
Case 15: ConvertIntoChar = "F"
Case 16: ConvertIntoChar = "G"
Case 17: ConvertIntoChar = "H"
Case 18: ConvertIntoChar = "I"
Case 19: ConvertIntoChar = "J"
Case 20: ConvertIntoChar = "K"
Case 21: ConvertIntoChar = "L"
Case 22: ConvertIntoChar = "M"
Case 23: ConvertIntoChar = "N"
Case 24: ConvertIntoChar = "O"
Case 25: ConvertIntoChar = "P"
Case 26: ConvertIntoChar = "Q"
Case 27: ConvertIntoChar = "R"
Case 28: ConvertIntoChar = "S"
Case 29: ConvertIntoChar = "T"
Case 30: ConvertIntoChar = "U"
Case 31: ConvertIntoChar = "V"
Case 32: ConvertIntoChar = "W"
Case 33: ConvertIntoChar = "X"
Case 34: ConvertIntoChar = "Y"
Case 35: ConvertIntoChar = "Z"
End Select
End Function

Private Sub Form_Terminate()
End Sub

GeneralRe: My code Pin
terwin17-Jun-05 7:11
memberterwin17-Jun-05 7:11 
GeneralRe: My code Pin
new2unix@gmail.com23-Jan-06 4:52
membernew2unix@gmail.com23-Jan-06 4:52 
GeneralRe: My code Pin
terwin23-Jan-06 4:59
memberterwin23-Jan-06 4:59 
GeneralRe: My code Pin
new2unix@gmail.com23-Jan-06 5:03
membernew2unix@gmail.com23-Jan-06 5:03 
GeneralRe: My code Pin
terwin23-Jan-06 5:08
memberterwin23-Jan-06 5:08 

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
Web01 | 2.8.151126.1 | Last Updated 23 Apr 2005
Article Copyright 2005 by
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid