字符串到十六进制-十六进制到字符串转换
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了字符串到十六进制-十六进制到字符串转换相关的知识,希望对你有一定的参考价值。
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.
public string ConvertStringToHex(string asciiString) { string hex = ""; foreach (char c in asciiString) { int tmp = c; hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString())); } return hex; } public string ConvertHexToString(string HexValue) { string StrValue = ""; while (HexValue.Length > 0) { StrValue += System.Convert.ToChar(System.Convert.ToUInt32(HexValue.Substring(0, 2), 16)).ToString(); HexValue = HexValue.Substring(2, HexValue.Length - 2); } return StrValue; }
以上是关于字符串到十六进制-十六进制到字符串转换的主要内容,如果未能解决你的问题,请参考以下文章