Home  >  Article  >  Backend Development  >  Detailed explanation of the sample code for obtaining Chinese character hexadecimal Unicode encoding string in c#

Detailed explanation of the sample code for obtaining Chinese character hexadecimal Unicode encoding string in c#

黄舟
黄舟Original
2017-03-27 11:54:171657browse

The following editor will bring you an example of c# realizing the acquisition of Chinese character hexadecimal Unicode encoding string. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor and take a look.

1. Convert Chinese characters to hexadecimal UNICODE encoded string

 /// 
  /// ////
  /// 
  /// 
  /// 
  public string CharacterToCoding(string character)
  {
   string coding = "";

   for (int i = 0; i < character.Length; i++)
   {
    byte[] bytes = System.Text.Encoding.Unicode.GetBytes(character.Substring(i, 1));

    //取出二进制编码内容 
    string lowCode = System.Convert.ToString(bytes[0], 16);

    //取出低字节编码内容(两位16进制) 
    if (lowCode.Length == 1)
    {
     lowCode = "0" + lowCode;
    }

    string hightCode = System.Convert.ToString(bytes[1], 16);

    //取出高字节编码内容(两位16进制) 
    if (hightCode.Length == 1)
    {
     hightCode = "0" + hightCode;
    }

    coding += (hightCode + lowCode);

   }

   return coding;
  }

2. Hexadecimal UNICODE encoding Convert string to Chinese characters

 /// 
  /// //
  /// 
  /// 
  /// 
  public string UnicodeToCharacter(string text)
  {
   byte[] arr = HexStringToByteArray(text);

   System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();

   string str = converter.GetString(arr);


   return str;
  }

The above is the detailed content of Detailed explanation of the sample code for obtaining Chinese character hexadecimal Unicode encoding string in c#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn