Blue Theme Orange Theme Green Theme Red Theme
 
Discover the top 5 tips for understanding .NET Interop
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Chart
Search :       Advanced Search »
Home » How do I » How to convert string to hexadecimal and vice versa

How to convert string to hexadecimal and vice versa

This article shows you how to convert string to hexadecimal and vice versa. I did this program for my smart Card application.

Page Views : 351496
Downloads : 2488
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
Hex Converter.zip
 
 
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
Nevron Chart
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

Introduction

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 code

The main code used for the conversion is from the Microsoft.VisualBasic class. 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;

} 

 

Ony two functions I used for this application

      

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

 

Data_Asc_Hex(data)

 

(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;
}

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 Article Extensions
Contents added by vadmal on Jan 07, 2010
Download File: HexConverter.zip
Hey Joshy,

I rewrote the code and took out the dependency on the Microsoft.VisualBasic DLL.
This is for the C# purists out there!

Thanks for your article!

 [Top] Rate this article
 
 About the author
 
Joshy George
Joshy George , has over 5 years in Microsoft technologies. Certified Microsoft Professional with Master Degree in Computers. Working with Infogenie ITAS Pvt.Ltd,Infopark,Kochin,India, as a Sr.Software Engineer and have good experience in C#,ASP.net ,VB, ASP, SQL Server, smart card application,Tapii,Com ctrls.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments
converter by Nancy On March 22, 2007
I like the code & especially your comment. Hope to see more of your samples. ;) Thanks! NG
Reply | Email | Modify 
Nice work.... by alex On December 9, 2008
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....
Reply | Email | Modify 
hex converter by jon On April 15, 2009
would there be a way to use a customized ascii table for this converter or to specify hex values? Thanks in advance
Reply | Email | Modify 
arabic character by c-sharp On July 5, 2009

I have try this code with arabic character but it didn't worked 

Reply | Email | Modify 
hello by Raaj On July 8, 2009
this works but dont with CR carriage return .
Reply | Email | Modify 
Hex file by Aru On January 7, 2010
How to read hex file in text format ?
Reply | Email | Modify 
Why not System.Text.Encoding.Unicode.GetBytes(yourString) by rod On March 10, 2010
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?
Reply | Email | Modify 
a program by manal On March 29, 2010

i need a program to convert hex to ascii in 8086 microprocessor

Reply | Email | Modify 
Thanx !!! by Raanan On February 16, 2011
work like a charm !!!
Reply | Email | Modify 
asm by lola On June 3, 2011
as would be the hex code to assembler Strine urgent
Reply | Email | Modify 
DevExpress Free UI Controls
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.