String to Hex - Hex to String Convert


/ Published in: C#
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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

You need to login to post a comment.