This article shows you how to convert string to hexadecimal and vice versa.
I did this program for my smart Card application. Earlier, I was using visual basic coding but this time I have to change my code into C#. Usually smart card application is writing data in hex format. I do not have a good hex converter to help me do those conversions that's why I made this.
Using the codeThe main code used for the conversion is from the Microsoft.VisualBasicclass. Below is the code that is used to convert a string to hexadecimal format. We can't direct convert all characters in to hexadecimal format (eg:@#$%^&*()) that's why firstly I take ASCII value of the character, and then convert ASCII value into hexadecimal format.
- //For this I made while loop
- while (Data.Length > 0)
- {
- //first I take each character using substring
- sValue= Data.Substring(0, 1).ToString()
- //then convert character into ascii.
- sValue= Strings.Asc(sValue)
- //then convert ascii value into Hex Format
- sValue = Conversion.Hex(sValue)
- //after converting remove the character.
- Data = Data.Substring(1, Data.Length - 1);
- sHex = sHex + sValue;
- }
Data_Hex_Asc(data)
(This Function for Converting data into hex format
- public string Data_Hex_Asc(ref string Data)
- {
- string Data1 = "";
- string sData = "";
- while (Data.Length > 0)
- //first take two hex value using substring.
- //then convert Hex value into ascii.
- //then convert ascii value into character.
- {
- Data1 = System.Convert.ToChar(System.Convert.ToUInt32(Data.Substring(0, 2), 16)).ToString();
- sData = sData + Data1;
- Data = Data.Substring(2, Data.Length - 2);
- }
- return sData;
- }
(This Function for Converting hex into data )
- public string Data_Asc_Hex(ref string Data)
- {
- //first take each charcter using substring.
- //then convert character into ascii.
- //then convert ascii value into Hex Format
- string sValue;
- string sHex = "";
- while (Data.Length > 0)
- {
- sValue = Conversion.Hex(Strings.Asc(Data.Substring(0, 1).ToString()));
- Data = Data.Substring(1, Data.Length - 1);
- sHex = sHex + sValue;
- }
- return sHex;
- }

Ramesh PalaniappanPosted Aug 29, 2016, 2:33 AM
Nice
MichaelPosted May 9, 2013, 1:17 PM
This way is easier: string text = "123ABC89"; int num = Int32.Parse(text, System.Globalization.NumberStyles.HexNumber); string newText = string.format("{0:X8}", num);
Tawaz teePosted Oct 9, 2012, 6:17 AM
I downloaded the hexConverter.zip, there is a little bug in your code: The bug goes like, its not converting Hexadecimal to string as desired because HexConvert.HexadecimalToString(sData) is taking a wrong string,it is taking values from txtData_To_Hex.Text instead of rchData.Text; Thanks for your code, its really helpful
Yacdani LopezPosted Apr 21, 2012, 4:37 PM
Thanks.
lola lolaaPosted Jun 3, 2011, 1:35 AM
as would be the hex code to assembler Strine urgent
RaananPosted Feb 16, 2011, 3:10 PM
work like a charm !!!
manal alabriPosted Mar 29, 2010, 3:27 AM
i need a program to convert hex to ascii in 8086 microprocessor
rod boggessPosted Mar 10, 2010, 12:41 PM
You can improve this by converting the entire string into an array of bytes first, then looping through each byte and calling conversion.Hex(yourBytes(index)). The only problem with this method (which brought me here) is when the hex output includes a leading zero. Do you know how to format the hex output?
Aru SupraPosted Jan 7, 2010, 8:25 AM
How to read hex file in text format ?
RaajPosted Jul 8, 2009, 10:39 AM
this works but dont with CR carriage return .
c-sharpPosted Jul 5, 2009, 7:17 PM
I have try this code with arabic character but it didn't worked
jonPosted Apr 15, 2009, 1:13 PM
would there be a way to use a customized ascii table for this converter or to specify hex values? Thanks in advance
alex prvnPosted Dec 9, 2008, 2:43 PM
hello friend... i am developing a application for smart card.. i got strucked in writing datas in to smartcard.. i am using 64 k card.... help me with source code for writing datas like name, address, a picture in to a smart card....
Nancy GarciaPosted Mar 22, 2007, 5:41 PM
I like the code & especially your comment. Hope to see more of your samples. ;) Thanks! NG