Posted By

emregulcan on 06/29/10


Tagged

hex String convert


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

emregulcan
Tyster


String to Hex - Hex to String Convert


 / Published in: C#
 

You can convert a string to hex or vice-versa with this methods. Example for real world; You want set / get some data from URL, but if your data has some special chars (like ^$ %) it'll be problem... In this case, you can use this methods and convert your data to hex.

  1. public string ConvertStringToHex(string asciiString)
  2. {
  3. string hex = "";
  4. foreach (char c in asciiString)
  5. {
  6. int tmp = c;
  7. hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
  8. }
  9. return hex;
  10. }
  11.  
  12. public string ConvertHexToString(string HexValue)
  13. {
  14. string StrValue = "";
  15. while (HexValue.Length > 0)
  16. {
  17. StrValue += System.Convert.ToChar(System.Convert.ToUInt32(HexValue.Substring(0, 2), 16)).ToString();
  18. HexValue = HexValue.Substring(2, HexValue.Length - 2);
  19. }
  20. return StrValue;
  21. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: annupam2003 on December 8, 2013

internal protected string ConvertStringToHex(string asciiString) { StringBuilder hex = new StringBuilder(); foreach (char c in asciiString) hex.AppendFormat("{0:x2}", (uint)c); return hex.ToString(); }

    public string ConvertHexToString(string HexValue)
    {
        StringBuilder StrValue = new StringBuilder();
        for(int i=0;i

You need to login to post a comment.