C# hex to ascii

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”

Leave a Comment