String and its code conversion in C ා

About character encoding and how to deal with unicode in the program, I want to write an article to summarize and study, but I find that what has been discussed by predecessors is very perfect, no longer repeated, you can refer to: http://www.regexlab.com/zh/encoding.htm.

In C + +, characters can be divided into char and wchar. Correspondingly, strings can be divided into string and wstring. In C, string is a unicode string, and each char is 16 bits.

The string constants in the source file will be automatically converted to unicode encoding (utf16). With Text.Encoding, the conversion between different encodings can be realized.

  1. using System;  
  2. using System.Text;  
  3.   
  4. namespace test  
  5. {  
  6.     class Program  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             string u16s = "Forget it abc"//The default character encoding is unicode, or utf16  
  11.   
  12.             //4 codes  
  13.               Encoding utf8 = Encoding.UTF8;  
  14.             Encoding utf16 = Encoding.Unicode;   
  15.             Encoding gb = Encoding.GetEncoding("gbk");   
  16.             Encoding b5 = Encoding.GetEncoding("big5");  
  17.   
  18.             //Convert to four encoded byte streams  
  19.             byte[] u16bytes = utf16.GetBytes(u16s);  
  20.             byte[] u8bytes = Encoding.Convert(utf16, utf8, u16bytes);  
  21.             byte[] gbytes = Encoding.Convert(utf16, gb, u16bytes);  
  22.             byte[] bbytes = Encoding.Convert(utf16, b5, u16bytes);  
  23.   
  24.             Console.Write("unicode: ");  
  25.             foreach (byte c in u16bytes)  
  26.             {  
  27.                 Console.Write(((int)c).ToString("x") + " ");  
  28.             }  
  29.             Console.WriteLine();  
  30.   
  31.             Console.Write("utf8: ");  
  32.             foreach(byte c in u8bytes)  
  33.             {  
  34.                 Console.Write(((int)c).ToString("x") + " ");  
  35.             }  
  36.             Console.WriteLine();  
  37.   
  38.             Console.Write("gbk: ");  
  39.             foreach (byte c in gbytes)  
  40.             {  
  41.                 Console.Write(((int)c).ToString("x") + " ");  
  42.             }  
  43.             Console.WriteLine();  
  44.   
  45.             Console.Write("big5: ");  
  46.             foreach (byte c in bbytes)  
  47.             {  
  48.                 Console.Write(((int)c).ToString("x") + " ");  
  49.             }  
  50.             Console.WriteLine();  
  51.   
  52.             //Get four encoded string s  
  53.             string u8s = utf8.GetString(u8bytes);  
  54.             string gs = gb.GetString(gbytes);  
  55.             string bs = b5.GetString(bbytes);  
  56.   
  57.             Console.WriteLine("unicode: " + u16s + " " + u16s.Length.ToString());  
  58.             Console.WriteLine("utf8: " + u8s + " " + u16s.Length.ToString());  
  59.             Console.WriteLine("gbk: " + gs + " " + gs.Length.ToString());  
  60.             Console.WriteLine("big5: " + bs + " " + bs.Length.ToString());  
  61.   
  62.             Console.Write("unicode: ");  
  63.             foreach (char c in u16s)  
  64.             {  
  65.                 Console.Write(((int)c).ToString("x") + " ");  
  66.             }  
  67.             Console.WriteLine();  
  68.   
  69.             Console.Write("utf8: ");  
  70.             foreach (char c in u8s)  
  71.             {  
  72.                 Console.Write(((int)c).ToString("x") + " ");  
  73.             }  
  74.             Console.WriteLine();  
  75.   
  76.             Console.Write("gb2312: ");  
  77.             foreach (char c in gs)  
  78.             {  
  79.                 Console.Write(((int)c).ToString("x") + " ");  
  80.             }  
  81.             Console.WriteLine();  
  82.   
  83.             Console.Write("big5: ");  
  84.             foreach (char c in bs)  
  85.             {  
  86.                 Console.Write(((int)c).ToString("x") + " ");  
  87.             }  
  88.             Console.WriteLine();  
  89.   
  90.             Console.ReadKey();  
  91.         }  
  92.     }  
  93. }  

 

//Output of the above program:

//Here are four types of encoded byte strings

unicode: d8 5f 18 8a 86 4e 4a 55 61 0 62 0 63 0
utf8: e5 bf 98 e8 a8 98 e4 ba 86 e5 95 8a 61 62 63
gbk: cd fc d3 9b c1 cb b0 a1 61 62 63
big5: a7 d1 b0 4f a4 46 b0 da 61 62 63

 

//Four strings from GetString() method
unicode: forget abc 7
utf8: forget abc 7
gbk: forget abc 7
big5: forget abc 7

 

//Character encoding in four string s
unicode: 5fd8 8a18 4e86 554a 61 62 63
utf8: 5fd8 8a18 4e86 554a 61 62 63
gb2312: 5fd8 8a18 4e86 554a 61 62 63
big5: 5fd8 8a18 4e86 554a 61 62 63

 

As you can see, using the GetString method, the strings obtained are all unicode encoded, that is to say, its function is to "decode" various encoded byte arrays into a unicode string.

Keywords: encoding

Added by gordonrp on Sun, 03 May 2020 08:57:57 +0300