C # byte array converted to string

Source: Internet
Author: User

C # byte array converted to string

If you want to find a way to convert between strings and byte arrays from the System.String class, I'm afraid you'll be disappointed. To make such a transition, we had to use another class: System.Text.Encoding. The class provides the 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 does not seem to have a usable constructor, but we can find several default Encoding, Encoding.default (Gets the encoding of the system's current ANSI code page), Encoding.ascii (gets 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 Encoding of UTF-8 format). Here the main talk about the difference between Encoding.default and encoding.unicode for conversion.

In the process of converting a string into a byte array, Encoding.default will each single-byte character, such as half-width in English, instead of each double-byte character, such as Kanji. Encoding.unicode will convert them to two bytes. We can get a simple look at the conversion method, and the difference between using Encoding.default and Encodeing.unicode:

  1. private void Teststringbytes () {
  2. String s = "C # language";
  3. Byte[] B1 = System.Text.Encoding.Default.GetBytes (s);
  4. byte[] b2 = System.Text.Encoding.Unicode.GetBytes (s);
  5. String t1 = " ", t2 = "";
  6. foreach (Byte B in B1) {
  7. T1 + = b.tostring ("") + "";
  8. }
  9. foreach (Byte b in B2) {
  10. T2 + = b.tostring ("") + "";
  11. }
  12. This.textBox1.Text = "";
  13. This.textBox1.AppendText ("B1. Length = "+ b1.  Length + "\ n");
  14. This.textBox1.AppendText (t1 + "\ n");
  15. This.textBox1.AppendText ("B2. Length = "+ b2.  Length + "\ n");
  16. This.textBox1.AppendText (t2 + "\ n");
  17. }

The operation result is as follows, do not say detailed, I believe everybody already understood.

    1. B1. Length = 6
    2. 67 35 211 239 209 212
    3. B2. Length = 8
    4. 67 0 35 0 237 139 0 138

Converts a C # byte array into a string, using the Encoding class's String GetString (byte[]) or a string GetString (byte[], int, int) method, which Encoding or is determined by the encoding. Add the following statement as an instance in the Teststringbytes () function:

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

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

C # byte array converted to string

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.