Convert Unicode to string C#(with string to unicode)

Lionsure 2020-08-10 Original by the website

Writing programs related to text processing, converting Unicode to string or converting string to Unicode encoding, is a code that is often written. If you want to distinguish between DBCS and English, you also need to use the DBCS and English Unicode code range.

String conversion to Unicode is easier to achieve. Unicode to string is a little more complicated. It needs to be converted to hexadecimal, then converted to binary bitstream, and finally converted to string. The implementation codes for their conversion are listed below.

 

1. Convert Unicode to string C#

The following methods can handle Unicode strings with 0x and without 0x, but for statistics, some with 0x and some without it are not allowed, either all or none.

using System.Globalization;

/// <summary>
       /// Convert Unicode to string C#
       /// </summary>
       /// <param name="text">Unicode</param>
       /// <returns>String</returns>

       public string UnicodeToString(string text)
       {
              if (string.IsNullOrEmpty(text)) return null;

       string temp = null;
              bool flag = false;

       int len = text.Length / 4;
              if (text.StartsWith("0x") || text.StartsWith("0X"))
              {
                     len = text.Length / 6;//0x in Unicode string
                     flag = true;
              }

       StringBuilder sb = new StringBuilder(len);
              for (int i = 0; i < len; i++)
              {
                     if (flag)
                            temp = text.Substring(i * 6, 6).Substring(2);
                     else
                            temp = text.Substring(i * 4, 4);

              byte[] bytes = new byte[2];
                     bytes[1] = byte.Parse(int.Parse(temp.Substring(0, 2), NumberStyles.HexNumber).ToString());
                     bytes[0] = byte.Parse(int.Parse(temp.Substring(2, 2), NumberStyles.HexNumber).ToString());
                     sb.Append(Encoding.Unicode.GetString(bytes));
              }
              return sb.ToString();
       }

Call:

string strUni="0x0055,0x006e,0x0069,0x0063,0x006f,0x0064,0x0065,0x0020,0x0074,0x006f,
0x0074,0x0072,0x0069,0x006e,0x0067,0x0020,0x0043,0x0023"
;

Or

string strUni = "0055006e00690063006f0064006500200074006f00200073007400720069006e0067002000430023";

UnicodeToString(strUni)

Output result: Unicode to string C#

 

 

2. Convert string to unicode C#

/// <summary>
       /// Convert string to unicode C#
       /// </summary>
       /// <param name="text">String</param>
       /// <returns>Unicode encoding</returns>

       public string StringToUnicode(string text)
       {
              bool flag = false;
              string strUni = null;
              string temp = null;

       foreach (char c in text)
              {
                     temp = string.Format("{0:x}", (int)c);

              if (temp.Length < 3)
                            temp = "00" + temp;

              if (flag)
                            strUni += ",0x" + temp;
                     else
                            strUni += "0x" + temp;

              if (!flag)
                            flag = true;
              }
              return strUni;
       }

string str = "Convert string to unicode C#";

StringToUnicode(str);

 

Output Unicode encoding:

0x0043,0x006f,0x006e,0x0076,0x0065,0x0072,0x0074,0x0020,0x0073,0x0074,0x0072,0x0069,
0x006e,0x0067,0x0020,0x0074,0x006f,0x0020,0x0075,0x006e,0x0069,0x0063,0x006f,0x0064,0x0065,
0x0020,0x0043,0x0023

 

Convert DBCS character to Unicode directly call the above method, you can get the corresponding Unicode encoding.