Solved

Convert Hex string to ascii character in vb.net2010

Posted on 2011-09-15
2
2,379 Views
Last Modified: 2012-05-12
There is an Hex string "20205955204D494E4720202020202020", it was converted from the Hex(a(i)) (a is byte).

Now I need to convert the Hex dtring back to ascii character, such as "abcd..."

Any suggestion, thanks.
0
Comment
Question by:turbot_yu
2 Comments
 
LVL 10

Accepted Solution

by:
gaurav05 earned 500 total points
ID: 36547690
Hi,

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;
}
0
 
LVL 40
ID: 36547729
That was an interesting exercice. Thanks for that question.

Dim hexString As String = "20205955204D494E4720202020202020"
Dim c(CInt(hexString.Length / 2)) As Char
Dim result As String

For x As Integer = 0 To hexString.Length - 4 Step 2
   c(CInt(x / 2)) = Chr(CInt("&H" & hexString.Substring(x, 2)))
Next

result = New String(c)

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question