C # byte array conversion to string analysis

Source: Internet
Author: User

The C # language is still a more common thing, where we mainly introduce C # byte arrays into strings, including the introduction of string conversions to byte arrays.

C # byte array converted to string

If you also want to find a way to convert between a string and a byte array from the System.String class, I'm afraid you'll be disappointed. To make this transition, we have to draw on another class: System.Text.Encoding. This class provides a bye[] GetBytes (String) method to convert a string to a byte array and a string GetString (byte[]) method to convert a C # byte array to a string.

The System.Text.Encoding class seems to have no constructors available, but we can find a few default Encoding, that is, Encoding.default (get the encoding of the system's current ANSI code page), Encoding.ascii (get 7-bit ASCII Character set encoding), Encoding.unicode (Get encoding in Unicode format in Little-endian byte order), ENCODING.UTF7 (Get encoding in UTF-7 format), Encoding.UTF8 (get UTF-8 format of the code) and so on. The main point here is the difference between Encoding.default and encoding.unicode for conversion.

In the process of converting a string to a byte array, Encoding.default converts each single-byte character, such as Half-width English, to each double-byte character, such as Kanji. And Encoding.unicode converts them all to two bytes. We can use the following simple understanding of the conversion method, as well as the difference between using Encoding.default and Encodeing.unicode:

private void TestStringBytes() {
string s = "C#语言";
byte[] b1 = System.Text.Encoding.Default.GetBytes(s);
byte[] b2 = System.Text.Encoding.Unicode.GetBytes(s);
string t1 = "", t2 = "";
foreach (byte b in b1) {
t1 += b.ToString("") + " ";
}
foreach (byte b in b2) {
t2 += b.ToString("") + " ";
}
this.textBox1.Text = "";
this.textBox1.AppendText("b1.Length = " + b1.Length + "\n");
this.textBox1.AppendText(t1 + "\n");
this.textBox1.AppendText("b2.Length = " + b2.Length + "\n");
this.textBox1.AppendText(t2 + "\n");
}

The results of the operation are as follows, do not say detailed, I believe we have understood.

b1.Length = 6
67 35 211 239 209 212
b2.Length = 8
67 0 35 0 237 139 0 138

Converts a C # byte array to a string, using the string GetString (byte[] or string GetString (byte[), int, int) method of the Encoding class, and what Encoding or encoding is used to specify. Add the following statement as an instance in the Teststringbytes () function:

byte[] bs = {97, 98, 99, 100, 101, 102};
string ss = System.Text.Encoding.ASCII.GetString(bs);
this.textBox1.AppendText("The string is: " + ss + "\n");

The result of the run is: the string is:abcdef

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.