C# – Converting a string of ASCII into normal string C#

asciic

Possible Duplicate:
.NET Convert from string of Hex values into Unicode characters (Support different code pages)

Looking to convert a string containing an ASCII string into text, i seem to be only be able to find System.Text.ASCIIEncoding.ASCII.GetString which converts from a Byte[] but in this circumstance I would like to be able to do it from a string.

its a string containing ASCII hex: For example : ASCI = 47726168616D would equal Graham

Are there any built in functions for this? help would be appreciated, thank you.

Best Answer

private static string GetStringFromAsciiHex(String input)
{
    if (input.Length % 2 != 0)
        throw new ArgumentException("input");

    byte[] bytes = new byte[input.Length / 2];

    for (int i = 0; i < input.Length; i += 2)
    {
        // Split the string into two-bytes strings which represent a hexadecimal value, and convert each value to a byte
        String hex = input.Substring(i, 2);
        bytes[i/2] = Convert.ToByte(hex, 16);                
    }

    return System.Text.ASCIIEncoding.ASCII.GetString(bytes);
}