C# output binary string(stream)

Lionsure 2020-08-14 Original by the website

Usually writing programs rarely output binary strings, most of which output character, integer, date, etc. In addition, the frequency of direct manipulation of binary is much lower than that of manipulation of characters and integers, so there are relatively few articles about C# output binary stream. However, sometimes the binary stream is directly manipulated. For example, when splitting a file, it is necessary to check whether some characters in the binary stream are DBCS characters to prevent truncating a DBCS character and generating garbled characters.

You can use the ToString(byte, 2) method to output binary strings in C#. Byte is an English binary stream, a half-byte binary stream of DBCS characters, and so on. Only one byte can be output at a time, just connect the binary strings output each time.

 

The method of C# output binary string(stream):

First get the binary stream of string, and then output the binary code of each character in a loop and merge it into a string. In order to facilitate identification, each binary code is separated by a comma. The implementation code is as follows:

/// <summary>
       /// C# output binary string(stream)
       /// </summary>
       /// <returns>Binary string</returns>

       private string OutPutBinary()
       {
              string text = "C# output binary string";
              byte[] arrByte = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(text);
              string byteStr = null;

       foreach (byte b in arrByte)
              {
                     if (byteStr == null)
                            byteStr = Convert.ToString(b, 2);
                     else
                            byteStr += ", " + Convert.ToString(b, 2);
              }
              return byteStr;
       }

Call: Response.Write("<br />" + OutPutBinary());

Output result

1000011, 100011, 100000, 1101111, 1110101, 1110100, 1110000, 1110101, 1110100, 100000, 1100010, 1101001, 1101110, 1100001, 1110010, 1111001, 100000, 1110011, 1110100, 1110010, 1101001, 1101110, 1100111

 

It can be seen from the output binary code that DBCS characters are composed of 8 bits, and the highest bit(the seventh bit) of each byte of each DBCS character is 1; English letters and special characters do not have 8 bits, because the front is 0, they are omitted when outputting, and their highest bit is not 1.