Solved

Convert Hex string to ascii character in vb.net2010

Posted on 2011-09-15
2
2,295 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

930 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now