Analyze problems
The conversion between a string and a byte array actually represents the conversion between information in the real world and that in the digital world. To understand the mechanism, you need to first understand several concepts.
1. Bit.
Bit refers to a single bit, which is the closest physical unit stored in a computer. The current computer system uses binary logic, that is, a basic unit can store two types of values: 0 and 1. This is because the 0 and 1 mechanisms can be represented by a variety of physical systems, such as high voltage and low level, diode conduction and shutdown, and magnetic field cathode and negative electrode. In short, a bit is a binary bit.
2. bytes.
A byte consists of eight bits in C. Its value can be 0 ~ A positive number of 255, but in C #, it is not allowed to implicitly assign a positive value to a Byte variable. The following code will cause compilation errors:
int i=10;int j=0;byte b=i-j;
3. encoding.
Encoding, which can be called the transfer mechanism of digital information and real information. A kind of encoding usually defines a character set conversion principle, commonly used encoding methods include utf8, gb2312, utf7, Unicode, etc., in the UTF-8, the character uses 1 ~ Encode six 8-bit sequences. In a sequence of 8-bit bytes, the byte height is 0, and the other seven digits are used for character value encoding. In a sequence of n (n> 1) 8-bit bytes, the Top N bits in the initial 8-bit bytes are 1, followed by 0, the remaining bit of this Byte contains the bit of the encoded character value. The maximum bit of all the 8-bit bytes is 1, and the next bit is 0. The remaining 6 bytes contain the encoded characters.
Shows the relationship between bit, byte, encoding, and string.
According to definition, the conversion of byte arrays and strings must involve some encoding method. Different encoding methods will lead to different conversion results. In C #, system. Text. encoding is used to manage common encodings. The following code converts a byte array to a string.
using System;using System.Text;namespace Test{ class StringByte { static void Main() { string str= "I am a string"; //A byte array into a string. //UTF8 encoding. byte[] utf8 = StringToByte(str, Encoding.UTF8); //GB2312 encoding byte[] gb2312 = StringToByte(str, Encoding.GetEncoding("GB2312")); //Unicode encoding byte[] unicode = StringToByte(str, Encoding.Unicode); Console.WriteLine(utf8.Length); Console.WriteLine(gb2312.Length); Console.WriteLine(unicode.Length); //Convert back to a string Console.WriteLine(ByteToString(utf8,Encoding.UTF8)); Console.WriteLine(ByteToString(gb2312,Encoding.GetEncoding("GB2312"))); Console.WriteLine(ByteToString(unicode,Encoding.Unicode)); Console.Read(); } //Convert a string to a byte array. static byte[] StringToByte(string str, Encoding encoding) { return encoding.GetBytes(str); } //Convert a byte array to a string static string ByteToString(byte[] byteArray,Encoding encoding) { return encoding.GetString(byteArray); } }}
The above program shows how to convert a string into a byte array using three common encoding methods, and then convert it back to a string. The program checks the length of the byte array. You can see that the byte array generated by different encoding methods is completely different. For specific encoding definitions, you can refer to the ISO and RFC standard documents. The following is the execution result of the program:
Answer
Conversion of strings and byte arrays depends on the encoding method. Different encoding standards use different algorithms. The system. Text. Encoding type provides support for most common Encoding algorithms for conversion between strings and byte arrays.