Click here to Skip to main content
13,247,005 members (89,511 online)
Click here to Skip to main content
Add your own
alternative version


101 bookmarked
Posted 6 May 2003

Converting Hexadecimal String to/from Byte Array in C#

, 6 May 2003
Rate this:
Please Sign up or sign in to vote.
Provides and demonstrates a hexadecimal string encoding/decoding class

Sample Image


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.


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;
    int discarded;
    txtByteCount.Text = ((int)HexEncoding.GetByteCount(hexString)).ToString();
    txtLength.Text = hexString.Length.ToString();
    byte[] byteArray = HexEncoding.GetBytes(hexString, out discarded);
    txtDiscard.Text = discarded.ToString();
    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.


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

Canada Canada
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionThank you! Pin
smirson24-May-17 3:15
membersmirson24-May-17 3:15 
GeneralMy vote of 2 Pin
HaiSonTran12-Apr-11 20:25
memberHaiSonTran12-Apr-11 20:25 
GeneralGreat job Pin
mapelo6329-Jan-10 17:23
membermapelo6329-Jan-10 17:23 
GeneralExplanation on "D3" Pin
jenniBrooklyn14-Dec-09 0:35
memberjenniBrooklyn14-Dec-09 0:35 
GeneralRe: Explanation on "D3" Pin
RascalRobot27-Jan-10 9:20
memberRascalRobot27-Jan-10 9:20 
GeneralLicense Pin
cesarfricks20-Aug-09 10:07
membercesarfricks20-Aug-09 10:07 
Questioncan some one send me the built executable? I am not able to build it :( Pin
vikrampatwardhan26-Jan-09 20:08
membervikrampatwardhan26-Jan-09 20:08 
GeneralFaster GetBytes() [modified] Pin
Mark Jerde22-Jan-09 12:16
memberMark Jerde22-Jan-09 12:16 
GeneralRe: Faster GetBytes() Pin
Member 334182311-Oct-09 19:20
memberMember 334182311-Oct-09 19:20 
GeneralReally good Pin
ankit_vyas2-Sep-08 16:50
memberankit_vyas2-Sep-08 16:50 
QuestionHow can I convert null to byte array? Pin
Nadya_Nos6-Aug-08 10:06
memberNadya_Nos6-Aug-08 10:06 
QuestionWhat about the speed and simplicity of my function ? [modified] Pin
gaby_la_star12-Nov-07 2:05
membergaby_la_star12-Nov-07 2:05 
Generalthank u so much neilck ! Pin
beginner_csharper15-Oct-07 2:25
memberbeginner_csharper15-Oct-07 2:25 
GeneralBitConverter Pin
Mihail Stefanov7-Jul-07 14:27
memberMihail Stefanov7-Jul-07 14:27 
GeneralSimpler, faster, better ... and most of all standard way Pin
Sébastien Ros14-May-07 6:36
memberSébastien Ros14-May-07 6:36 
GeneralRe: Simpler, faster, better ... and most of all standard way Pin
Stefan Gheorghe3-Jul-07 14:23
memberStefan Gheorghe3-Jul-07 14:23 
GeneralRe: Simpler, faster, better ... and most of all standard way Pin
Sebastien Ros11-Jul-07 1:31
memberSebastien Ros11-Jul-07 1:31 
GeneralRe: Simpler, faster, better ... and most of all standard way [modified] Pin
George Helyar5-Apr-08 7:33
memberGeorge Helyar5-Apr-08 7:33 
GeneralRe: Simpler, faster, better ... and most of all standard way Pin
j@mars16-Apr-08 5:44
memberj@mars16-Apr-08 5:44 
GeneralRe: Simpler, faster, better ... and most of all standard way Pin
George Helyar16-Apr-08 6:33
memberGeorge Helyar16-Apr-08 6:33 
GeneralRe: Simpler, faster, better ... and most of all standard way Pin
j@mars16-Apr-08 7:20
memberj@mars16-Apr-08 7:20 
GeneralRe: Simpler, faster, better ... and most of all standard way Pin
George Helyar16-Apr-08 8:43
memberGeorge Helyar16-Apr-08 8:43 
GeneralRe: Simpler, faster, better ... and most of all standard way Pin
ramaluciano30-Nov-08 10:12
memberramaluciano30-Nov-08 10:12 
GeneralRe: Simpler, faster, better ... and most of all standard way Pin
bobslayer31-Jul-07 6:20
memberbobslayer31-Jul-07 6:20 
GeneralPerfect Pin
Davepow165-Mar-07 10:21
memberDavepow165-Mar-07 10:21 
NewsAnother fast variant of mine.... [modified] Pin
RealFractalizeR4-Mar-07 10:32
memberRealFractalizeR4-Mar-07 10:32 
GeneralI think this is faster (tested) Pin
meraklideve25-Jun-08 4:40
membermeraklideve25-Jun-08 4:40 
GeneralRe: I think this is faster (tested) Pin
RealFractalizeR25-Jun-08 12:52
memberRealFractalizeR25-Jun-08 12:52 
Generalbig endian hex Pin
vadivelkumar22-Feb-07 0:28
membervadivelkumar22-Feb-07 0:28 
GeneralBuilt in .NET parse function that does this Pin
MGutman8-Jan-07 8:45
memberMGutman8-Jan-07 8:45 
GeneralRe: Built in .NET parse function that does this Pin
noGuns19-Apr-07 11:43
membernoGuns19-Apr-07 11:43 
GeneralRe: Built in .NET parse function that does this Pin
Mr Marchepane7-May-07 10:10
memberMr Marchepane7-May-07 10:10 
GeneralRe: Built in .NET parse function that does this Pin
Simon Capewell22-Nov-07 4:29
memberSimon Capewell22-Nov-07 4:29 
GeneralSaved me a bunch of time - thanks Pin
TimBSmith19715-Jan-07 14:36
memberTimBSmith19715-Jan-07 14:36 
Generalwonderful Pin
sinan12345617-Oct-06 6:04
membersinan12345617-Oct-06 6:04 
General'.' being not properly converted Pin
Hoodwink13-Sep-06 13:20
memberHoodwink13-Sep-06 13:20 
GeneralSimpler Pin
raytaller6-Jun-06 7:51
memberraytaller6-Jun-06 7:51 
GeneralRe: Simpler Pin
MGutman8-Jan-07 8:49
memberMGutman8-Jan-07 8:49 
GeneralIT students Pin
sunset_h22_m6-Apr-06 4:23
membersunset_h22_m6-Apr-06 4:23 
GeneralSystem.Uri.IsHexDigit Pin
Chirpychips3-Dec-05 4:06
memberChirpychips3-Dec-05 4:06 
GeneralThanks to all for performance tips Pin
neilck22-Jun-05 15:29
memberneilck22-Jun-05 15:29 
GeneralJust what I needed Pin
Joe Pardue16-Jun-05 6:05
memberJoe Pardue16-Jun-05 6:05 
GeneralGreatly Appreciated! Pin
Devon Knight13-Apr-05 14:58
memberDevon Knight13-Apr-05 14:58 
GeneralFastest I found.. Pin
stotti_no121-Mar-05 22:05
memberstotti_no121-Mar-05 22:05 
AnswerRe: Fastest I found.. Pin
tomstrummer28-Jul-07 8:07
membertomstrummer28-Jul-07 8:07 
Generalincrease speed Pin
CyberDEF3-Feb-05 12:02
memberCyberDEF3-Feb-05 12:02 
Generalthanks a lot Pin
wilson_o23-Jan-05 17:46
susswilson_o23-Jan-05 17:46 
Generalabout discarded Pin
yzh_x9-Jan-05 17:42
memberyzh_x9-Jan-05 17:42 
GeneralThanks yeah :) Pin
chuawenching9-Sep-04 17:17
memberchuawenching9-Sep-04 17:17 
This articles had saves my time of coding the hex to bytes. Thanks again Smile | :) I love the stringbuilder part...
GeneralImprove performance Pin
Esteves15-Nov-03 18:05
memberEsteves15-Nov-03 18:05 

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
Web04 | 2.8.171114.1 | Last Updated 7 May 2003
Article Copyright 2003 by neilck
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid