Posted by: Praveen | September 25, 2008

Sample C# code to convert Hexadecimal String to ASCII

The following C# code sample can be used for converting a Hexadecimal String(hex string) to ASCII.

private string HexString2Ascii(string hexString)
StringBuilder sb = new StringBuilder();
for (int i = 0; i <= hexString.Length - 2; i += 2)
sb.Append(Convert.ToString(Convert.ToChar(Int32.Parse(hexString.Substring(i, 2), System.Globalization.NumberStyles.HexNumber))));
return sb.ToString();



  1. Muito obrigado!

  2. Your Welcome!

  3. hi, would there be a better alternative other than stringbuilder? cos my hex string is very very very long. it would be best if it can build an unlimited amount.

  4. hey it’s okay. the problem doesn’t lie with the stringbuilder. thanks for the code! 🙂

  5. Hi,i need to convert the data “7820 6a75 6d70 6564 206f 7665 7220” into ascii using c# ,how is it done, can anyone help,please.

  6. thanks a lot

  7. This might cause problems if quotes itself (or some other special characters) are part of the string. A better way would be using bytes array and then using encoding to get string. For example i ran into an issue where this was the ascii result “I”ÊôL” but the hex value of 4994CAF44C would not translate into it using stringbuilder. Here’s my solution. Please let me know what you think. thx

    private static string HexString2Ascii(string hexString)
    byte[] tmp;
    int j = 0;
    tmp = new byte[(hexString.Length)/2];
    for (int i = 0; i <= hexString.Length – 2; i += 2)
    tmp[j] =(byte)Convert.ToChar(Int32.Parse(hexString.Substring(i, 2), System.Globalization.NumberStyles.HexNumber));

    return Encoding.GetEncoding(1252).GetString(tmp);

  8. thxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

  9. Thanks for the script !! It’s the only one I found working. Good job :p

  10. Thanks Geek ….it´s very important!!!!

  11. Thank you!

  12. Hi Geek,
    I am trying to convert raw hexadecimal data transmitted from a GPS device and insert into database. The sample data from the GPS handbook is pasted below
    0f14f650 – Latitude 253032016 = 25,3032016º N
    209cca80 – Longitude 547146368 = 54,7146368 º E

    When i am passing the value 0f14f650 in your function it returns me junk data instead of 253032016. Please let me know the reason for the same.


  13. I want to convert HexString to byte[] with out using loop , Is it posible?

  14. thanks much!

  15. It seems like you’ve gathered yourself quite the little following nowadays! I’m pleased to see

  16. Thx a lot!

  17. Nice!

  18. Hey there! I simply wish to give you a big thumbs up for your great info
    you have got here on this post. I will be coming
    back to your website for more soon.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: