14,664,191 members
Articles » General Programming » Algorithms & Recipes » Data Structures
Article
Posted 6 May 2003

855.4K views
103 bookmarked

Converting Hexadecimal String to/from Byte Array in C#

Rate this:
6 May 2003
Provides and demonstrates a hexadecimal string encoding/decoding class

Introduction

While the .NET framework provides methods to convert a byte array into a Hexadecimal string ( `byte.ToString(“X”) `), it is not so easy to convert a hexadecimal string back into a byte array. Such a function is useful when you need to backup data from your application on paper, such as an encryption key, and later, convert it back into data after the user types it in.

The `HexEncoding `class provided here, contains functions which allow for the conversion of a string in hexadecimal format into a byte array, and back. It also contains functions which lets you check the formatting of the string before conversion, and how many bytes a given string will produce.

Background

In a hexadecimal string, one byte is represented two hexadecimal characters. A hexadecimal character has a value of (A-F, 0-9).

e.g. string “01FFA0” is equivalent to `byte[] { 1, 255, 160 }`

Using the code

HexEncoding is the name of the class I created with static functions for hexadecimal string conversion.

Here’s a sample of how it is used, when the Convert button is clicked like on the screenshot:

```private void button1_Click(object sender, System.EventArgs e)
{
string hexString = txtHex.Text;
txtByteCount.Text = ((int)HexEncoding.GetByteCount(hexString)).ToString();
txtLength.Text = hexString.Length.ToString();
byte[] byteArray = HexEncoding.GetBytes(hexString, out discarded);
string temp = "";
for (int i=0; i<byteArray.Length; i++)
{
temp += byteArray[i].ToString("D3") + " ";
}
txtByte.Text = temp;
txtHex2.Text = HexEncoding.ToString(byteArray);
}```

`HexEncoding`.`GetByteCount(string hexString)` returns the number of bytes that will be generated from the hexString.

`HexEncoding.GetBytes(string hexString, out int discarded)` returns the byte array converted from the hexString, and the second parameter returns the number of non-hexadecimal characters that were ignored in the string. This includes dashes, whitespace, and letters after ‘F’.

`HexEncoding.ToString(byte[]) `returns the newly converted byte array back into string form. Notice the ‘-‘ characters are now missing.

The key function provided by the framework to convert a hexadecimal string to a single byte is this:

`// byte newByte = byte.Parse(hex, System.Globalization.NumberStyles.HexNumber);`

where “hex” is of the form “1A”, “00”, “FF”, etc.

Thanks to Polux on the .NET 247 newsgroups who posted the `int.Parse(...) `answer to another hexadecimal question.

A list of licenses authors might use can be found here

Share

No Biography provided

 I think this is faster (tested) meraklideve25-Jun-08 3:40 meraklideve 25-Jun-08 3:40
 Re: I think this is faster (tested) VladislavRastrusny25-Jun-08 11:52 VladislavRastrusny 25-Jun-08 11:52
 Built in .NET parse function that does this MGutman8-Jan-07 7:45 MGutman 8-Jan-07 7:45
 Re: Built in .NET parse function that does this noGuns19-Apr-07 10:43 noGuns 19-Apr-07 10:43
 Re: Built in .NET parse function that does this Mr Marchepane7-May-07 9:10 Mr Marchepane 7-May-07 9:10
 Re: Built in .NET parse function that does this Simon Capewell22-Nov-07 3:29 Simon Capewell 22-Nov-07 3:29
 Saved me a bunch of time - thanks TimBSmith19715-Jan-07 13:36 TimBSmith1971 5-Jan-07 13:36
 Didnt have to to do this pattern again - thanks! Tim.Smith@Venturality.com
 wonderful sinan12345617-Oct-06 5:04 sinan123456 17-Oct-06 5:04
 '.' being not properly converted Hoodwink13-Sep-06 12:20 Hoodwink1 3-Sep-06 12:20
 Simpler raytaller6-Jun-06 6:51 raytaller 6-Jun-06 6:51
 Re: Simpler MGutman8-Jan-07 7:49 MGutman 8-Jan-07 7:49
 IT students sunset_h22_m6-Apr-06 3:23 sunset_h22_m 6-Apr-06 3:23
 System.Uri.IsHexDigit Chirpychips3-Dec-05 3:06 Chirpychips 3-Dec-05 3:06
 Thanks to all for performance tips neilck22-Jun-05 14:29 neilck 22-Jun-05 14:29
 Just what I needed Joe Pardue16-Jun-05 5:05 Joe Pardue 16-Jun-05 5:05
 Greatly Appreciated! Devon Knight13-Apr-05 13:58 Devon Knight 13-Apr-05 13:58
 Fastest I found.. Sven So.21-Mar-05 21:05 Sven So. 21-Mar-05 21:05
 Re: Fastest I found.. tomstrummer28-Jul-07 7:07 tomstrummer 28-Jul-07 7:07
 increase speed CyberDEF3-Feb-05 11:02 CyberDEF 3-Feb-05 11:02
 thanks a lot wilson_o23-Jan-05 16:46 wilson_o 23-Jan-05 16:46
 Thanks yeah :) Chua Wen Ching9-Sep-04 16:17 Chua Wen Ching 9-Sep-04 16:17
 Improve performance Esteves15-Nov-03 17:05 Esteves 15-Nov-03 17:05
 Very good mikeperetz5-Sep-03 11:11 mikeperetz 5-Sep-03 11:11
 Last Visit: 25-Oct-20 7:16     Last Update: 25-Oct-20 7:16 Refresh ᐊ Prev123 Next ᐅ