[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Convert Hex string to ascii character in vb.net2010

Posted on 2011-09-15
2
Medium Priority
?
2,653 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 10

Accepted Solution

by:
gaurav05 earned 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

650 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