Click here to Skip to main content
Licence 
First Posted 22 Apr 2005
Views 139,984
Bookmarked 75 times

Hexadecimal, Binary, and Decimal conversions

By | 22 Apr 2005 | Article
This project contains a class aimed to all possible combinations for hexadecimal, binary, and decimal conversions.

Sample Image - BinaryAndHexConversions.jpg

Introduction

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

Conclusion

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

License

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

rudy.net

Web Developer

United States United States

Member



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
QuestionIs this DLL licensed freeware? PinmemberKhadar1231:31 31 Mar '09  
AnswerRe: Is this DLL licensed freeware? Pinmemberrudy.net10:06 31 Mar '09  
GeneralGenius!! Pinmemberindogerman23:11 12 Feb '09  
Generalunable to open file Pinmembermetoofaiza21:23 1 Nov '08  
Generalgood day!! PinmemberMark Arjim Parba17:50 15 Jun '08  
GeneralRe: good day!! Pinmemberrudy.net8:42 16 Jun '08  
QuestionCAN SHOW ME the CODE HEX to DEC in JAVA PinmemberranzJR18:55 22 Nov '07  
AnswerRe: CAN SHOW ME the CODE HEX to DEC in JAVA Pinmemberrudy.net1:17 26 Nov '07  
Generaldisplay a binary file in heaxdecimal PinmemberNEKSA0:13 16 Jul '06  
GeneralRe: display a binary file in heaxdecimal Pinmemberrudy.net3:51 17 Jul '06  
Generalsigned vs unsigned PinmemberTalal Sultan3:22 10 May '06  
GeneralRe: signed vs unsigned Pinmemberrudy.net18:55 10 May '06  
QuestionConvert from Hex into Binary Pinmemberrandacodeproject22:05 17 Apr '06  
AnswerRe: Convert from Hex into Binary Pinmemberrudy.net2:36 18 Apr '06  
Rany,
The conversion doesn't do any verification that the data passed is valid. Most likely the hex string passed to the conversion is not valid.
To get you going, just do a verification that the data is a valid hex number before you do the conversion. I will post a new release that verifies all inputs are valid before doing any conversions.
 
Regards,
Rudy.
GeneralUpdates to this component PinmemberEric Engler5:15 6 Apr '06  
GeneralRe: Updates to this component Pinmemberrudy.net12:29 6 Apr '06  
General.NET ver 2.0 Pinmembergordsh9:29 19 Jan '06  
GeneralRe: .NET ver 2.0 Pinmemberrudy.net9:41 19 Jan '06  
Generalsuggestions PinmemberEric Engler4:38 2 May '05  
GeneralMy code PinmemberDrGUI11:08 29 Apr '05  
GeneralRe: My code Pinmemberterwin6:11 17 Jun '05  
GeneralRe: My code Pinmembernew2unix@gmail.com3:52 23 Jan '06  
GeneralRe: My code Pinmemberterwin3:59 23 Jan '06  
GeneralRe: My code Pinmembernew2unix@gmail.com4:03 23 Jan '06  
GeneralRe: My code Pinmemberterwin4:08 23 Jan '06  

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
Web02 | 2.5.120529.1 | Last Updated 23 Apr 2005
Article Copyright 2005 by rudy.net
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid