C# hex to ascii

86,662

Solution 1

This code will convert the hex string into ASCII, you can copy paste this into a class and use it without instancing

public static string ConvertHex(String hexString)
{
    try
    {
        string ascii = string.Empty;

        for (int i = 0; i < hexString.Length; i += 2)
        {
            String hs = string.Empty;

            hs   = hexString.Substring(i,2);
            uint decval =   System.Convert.ToUInt32(hs, 16);
            char character = System.Convert.ToChar(decval);
            ascii += character;

        }

        return ascii;
    }
    catch (Exception ex) { Console.WriteLine(ex.Message); }

    return string.Empty;
}

Notes

2 = the no. of hexString chars used to represent an ASCII character.

System.Convert.ToUInt32(hs, 16) = "convert the base 16 hex substrings to an unsigned 32 bit int"

Solution 2

There are four three problems here:

  1. Since you're incrementing i by 2 on each iteration, you need to terminate at hexString.Length - 1. This doesn't actually matter; incrementing by two after the final iteration will bring the counter above the checked length regardless.
  2. You're taking the wrong number of characters from hexString.
  3. hs is never used.
  4. You're not appending anything to sb.

Try this:

for (int i = 0; i < hexString.Length; i += 2)
{
    string hs = hexString.Substring(i, 2);
    sb.Append(Convert.ToChar(Convert.ToUInt32(hs, 16)));
}

Note that there's no need to qualify the types with their namespace, System (assuming you've referenced it at the top of the file with a using statement).

Solution 3

String hs = hexString.Substring(i, i + 2);
System.Convert.ToChar(System.Convert.ToUInt32(hexString.Substring(0, 2), 16)).ToString();

Do you notice you're never using hs ??

And that you're converting the first 2 chars over and over?

Solution 4

Since you are incrementing your index by 2, you need to stop your loop one-before-the-end of the length of the string. Otherwise your last iteration of the loop will try to read characters past the end of the string.

for (int i = 0; i < hexString.Length - 1, i += 2)
Share:
86,662
Pete
Author by

Pete

Updated on November 08, 2020

Comments

  • Pete
    Pete over 3 years

    I'm trying to convert a String of hex to ASCII, using this:

    public void ConvertHex(String hexString)
    {
        StringBuilder sb = new StringBuilder();
    
        for (int i = 0; i < hexString.Length; i += 2)
        {
            String hs = hexString.Substring(i, i + 2);
            System.Convert.ToChar(System.Convert.ToUInt32(hexString.Substring(0, 2), 16)).ToString();
        }
        String ascii = sb.ToString();
        MessageBox.Show(ascii);
    }
    

    but I get an out or bounds exception, I'm sure its a glaring error but other code I have tried does not work either. What am I doing wrong?

  • Pete
    Pete about 13 years
    "Do you notice you're never using hs ??" yes that was a stupid mistake "And that you're converting the first 2 chars over and over?" no I hadn't lol
  • Pete
    Pete about 13 years
    Thanks that's almost it, I get an Overflow exception "too large or to small", will post a fix when I have one
  • Will Vousden
    Will Vousden about 13 years
    Where does the exception occur?
  • Pete
    Pete about 13 years
    Within "sb.Append(Convert.ToChar(Convert.ToUInt32(hs, 16)));" Value was either too small or too large for a charcter :/
  • configurator
    configurator about 13 years
    @Pete: Are you using string hs = hexString.Substring(i, 2); or your original, Substring(i, i + 2) which takes too many characters?
  • Will Vousden
    Will Vousden about 13 years
    @Pete: The code works for me (for well-formed hex input, that is). Can you post the complete code you're now using?
  • Chris Halcrow
    Chris Halcrow over 4 years
    Notes 2 = the no. of hexString chars used to represent an ASCII character. System.Convert.ToUInt32(hs, 16) means "convert the base 16 hex substrings to an unsigned 32 bit int"