An example of a conversion method between a string and hexadecimal

  • 2020-05-24 06:01:39
  • OfStack


/// <summary>
        /// < Function: Encode>
        ///  Effect: converts the string content to 16 Base data encoding, the inverse process is Decode
        ///  Parameter description: 
        /// strEncode  The original string that needs to be converted 
        ///  The conversion process is to convert the characters directly to Unicode character , Such as digital "3"-->0033, Chinese characters " I "-->U+6211
        ///  function decode The process is encode The inverse process .
        /// </summary>
        /// <param name="strEncode"></param>
        /// <returns></returns>
        public static string Encode(string strEncode)
        {
            string strReturn = "";//   Stores the transformed encoding 
            foreach (short shortx in strEncode.ToCharArray())
            {
                strReturn += shortx.ToString("X4");
            }
            return strReturn;
        }
        /// <summary>
        /// < Function: Decode>
        /// Role: 16 Base data encoding into a string, yes Encode The inverse process 
        /// </summary>
        /// <param name="strDecode"></param>
        /// <returns></returns>
        public static string Decode(string strDecode)
        {
            string sResult = "";
            for (int i = 0; i < strDecode.Length / 4; i++)
            {
                sResult += (char)short.Parse(strDecode.Substring(i * 4, 4), global::System.Globalization.NumberStyles.HexNumber);
            }
            return sResult;
        }


Related articles: