Click here to Skip to main content
13,349,033 members (70,844 online)
Click here to Skip to main content
Add your own
alternative version


81 bookmarked
Posted 22 Apr 2005

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

QuestionHow to read a text file consisting of alphanumeric characters, white spaces & punctuations and then convert the data into binary values using ASCII codes, in C language Pin
Member 1112036216-Oct-14 4:26
memberMember 1112036216-Oct-14 4:26 
AnswerRe: How to read a text file consisting of alphanumeric characters, white spaces & punctuations and then convert the data into binary values using ASCII codes, in C language Pin
rudy.net16-Oct-14 8:40
memberrudy.net16-Oct-14 8:40 
QuestionIs this DLL licensed freeware? Pin
Khadar12331-Mar-09 2:31
memberKhadar12331-Mar-09 2:31 
AnswerRe: Is this DLL licensed freeware? Pin
rudy.net31-Mar-09 11:06
memberrudy.net31-Mar-09 11:06 
GeneralGenius!! Pin
indogerman13-Feb-09 0:11
memberindogerman13-Feb-09 0:11 
Generalunable to open file Pin
metoofaiza1-Nov-08 22:23
membermetoofaiza1-Nov-08 22:23 
Generalgood day!! Pin
Mark Arjim Parba15-Jun-08 18:50
memberMark Arjim Parba15-Jun-08 18:50 
GeneralRe: good day!! Pin
rudy.net16-Jun-08 9:42
memberrudy.net16-Jun-08 9:42 
QuestionCAN SHOW ME the CODE HEX to DEC in JAVA Pin
ranzJR22-Nov-07 19:55
memberranzJR22-Nov-07 19:55 
AnswerRe: CAN SHOW ME the CODE HEX to DEC in JAVA Pin
rudy.net26-Nov-07 2:17
memberrudy.net26-Nov-07 2:17 
Generaldisplay a binary file in heaxdecimal Pin
NEKSA16-Jul-06 1:13
memberNEKSA16-Jul-06 1:13 
GeneralRe: display a binary file in heaxdecimal Pin
rudy.net17-Jul-06 4:51
memberrudy.net17-Jul-06 4:51 
Generalsigned vs unsigned Pin
Talal Sultan10-May-06 4:22
memberTalal Sultan10-May-06 4:22 
GeneralRe: signed vs unsigned Pin
rudy.net10-May-06 19:55
memberrudy.net10-May-06 19:55 
QuestionConvert from Hex into Binary Pin
randacodeproject17-Apr-06 23:05
memberrandacodeproject17-Apr-06 23:05 
AnswerRe: Convert from Hex into Binary Pin
rudy.net18-Apr-06 3:36
memberrudy.net18-Apr-06 3:36 
GeneralUpdates to this component Pin
Eric Engler6-Apr-06 6:15
memberEric Engler6-Apr-06 6:15 
GeneralRe: Updates to this component Pin
rudy.net6-Apr-06 13:29
memberrudy.net6-Apr-06 13:29 
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 
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 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 23 Apr 2005
Article Copyright 2005 by
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid