Solved

Convert Hex string to ascii character in vb.net2010

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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…

749 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