Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

How to convert string value to/from hex value in ASP.NET

Author: Rudy Pinson
Download Source Code :

This simple article will show how we can convert string value to hexadecimal value and hexadecimal value back to string.


Sometimes you may need to use hexadecimal values in your application, for example smart card applications uses data in hexadecimal format, but the input from a user will be a string of characters. First part of the following code snippet can be used to convert a string of characters to its ASCII representation and then convert this ASCII code to hexadecimal value. Second part of the code can be used to convert hexadecimal value to ASCII code and ASCII values to string of Characters. This conversion can be done with different techniques but the following code snippet will make this conversion a simple process. The code is available both in C# and VB.NET and you can also download complete source code as zip file.

Code for aspx page

    <form id="form1" runat="server">
        Write any string value
        <br />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Convert"
            onclick="Button1_Click" />
        <br />
        <asp:Label ID="Label1" runat="server" ></asp:Label>
        <br />
        <asp:Label ID="Label2" runat="server" ></asp:Label>
Code for C# code file

using System.Globalization;
protected void Button1_Click(object sender, EventArgs e)
    //code to convert string to hex
    string value = TextBox1.Text;
    string stringValue = "";
    string hexValue = "";
    for (int i = 0; i < value.Length; i++)
        int a = Convert.ToInt32(value[i]);
        stringValue += a;
    int intValue = Convert.ToInt32(stringValue);
    hexValue = intValue.ToString("X");
    Label1.Text = hexValue;
    //code to convert hex to string
    int intValueBack = int.Parse(hexValue, NumberStyles.HexNumber);
    string asciiString = intValueBack.ToString();
    string stringValueBack = "";
    for (int i = 0; i <= asciiString.Length - 1; i += 2)
        int a = Convert.ToInt32(asciiString.Substring(i, 2));
        char c = Convert.ToChar(a);
        stringValueBack = stringValueBack + c;
    Label2.Text = stringValueBack;
Code for VB.NET code file

Imports System.Globalization
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    'code to convert string to hex
    Dim value As String = TextBox1.Text
    Dim stringValue As String = ""
    Dim hexValue As String = ""
    For i As Integer = 0 To value.Length - 1
        Dim a As Integer = Convert.ToInt32(value(i))
        stringValue += a
    Dim intValue As Integer = Convert.ToInt32(stringValue)
    hexValue = intValue.ToString("X")
    Label1.Text = hexValue
    'code to convert hex to string
    Dim intValueBack As Integer = Integer.Parse(hexValue, NumberStyles.HexNumber)
    Dim asciiString As String = intValueBack.ToString()
    Dim stringValueBack As String = ""
    For i As Integer = 0 To asciiString.Length - 1 Step 2
        Dim a As Integer = Convert.ToInt32(asciiString.Substring(i, 2))
        Dim c As Char = Convert.ToChar(a)
        stringValueBack = stringValueBack + c
    Label2.Text = stringValueBack
End Sub
First get value from user and declare string variables for string and hexadecimal values. I have converted string value to its ASCII representation of integer value. Then I have converted this integer value to hexadecimal value simply using ToString() method with “X” as parameter.
To convert hexadecimal value to string value, I have first converted it to integer value. Then I have converted integer value to a string value named it as asciiString because this is a string of ASCII values. Then I get every two integers and convert it to character. Then I have created a string by concatenating these characters. 


Add Article Comment:
Name :
Email Address :
Comments :
<< How to load Web User Control using jQuery and WebMethod in ASP.NET

Disclaimer - Privacy
© 2002-2017