Solved

Convert Hex string to ascii character in vb.net2010

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

20 Experts available now in Live!

Get 1:1 Help Now