C # strings and byte arrays are converted to each other

Source: Internet
Author: User

Summary: It is unlikely that a method can be found directly from the System.String class to convert between a string and a byte array. To convert between them, you need to use a different type: System.Text.Encoding. This type provides a way to convert C # strings to byte arrays, as well as to convert C # byte arrays into strings.

The default constructor for the System.Text.Encoding type is not available, but the type provides several default static Properties. As follows:

1   //2         //summary:3         //gets the encoding of the ASCII (7-bit) character Set. 4         //5         //return result:6         //encoding of the ASCII (7-bit) character Set. 7          public StaticEncoding ASCII {Get; }8         //9         //summary:Ten         //gets the encoding of the UTF-16 format using the Big Endian byte order.  one         // a         //return result: -         //gets the encoded object in UTF-16 format that uses the Big Endian byte order.  -          public StaticEncoding Bigendianunicode {Get; } the         // -         //summary: -         //gets the encoding of the operating System's current ANSI code Page.  -         // +         //return result: -         //The encoding of the operating System's current ANSI code Page.  +          public StaticEncoding Default {Get; } a         // at         //summary: -         //gets the encoding in UTF-16 format using the Little-endian byte order.  -         // -         //return result: -         //encode in UTF-16 format using the Little-endian byte order.  -          public StaticEncoding Unicode {Get; } in         // -         //summary: to         //gets the encoding in UTF-32 format using the Little-endian byte order.  +         // -         //return result: the         //encodes an object in UTF-32 format using the Little-endian byte order.  *          public StaticEncoding UTF32 {Get; } $         //Panax Notoginseng         //summary: -         //gets the encoding for the UTF-7 format.  the         // +         //return result: a         //encoding of the UTF-7 format.  the          public StaticEncoding UTF7 {Get; } +         // -         //summary: $         //gets the encoding for the UTF-8 format.  $         // -         //return result: -         //encoding of the UTF-8 format.  the          public StaticEncoding UTF8 {Get; }
System.Text.Encoding

one, the string is converted into a byte array

1  //System.Text.Encoding.GetBytes (String Str)2             //1. String conversion byte array3 4             stringSTR1 ="character string1";5             varStrToBytes1 =System.Text.Encoding.UTF8.GetBytes (str1);6 7             stringSTR2 ="character string2";8             varStrToBytes2 =System.Text.Encoding.Default.GetBytes (str2);9 Ten             stringSTR3 ="character String3"; one             varStrToBytes3 =System.Text.Encoding.Unicode.GetBytes (str3); a  -             stringSTR4 ="character String4"; -             varStrToBytes4 =System.Text.Encoding.ASCII.GetBytes (str4); the  -             stringSTR5 ="character String5"; -             varSTRTOBYTES5 =System.Text.Encoding.UTF32.GetBytes (str5); -  +             stringSTR6 ="character String6"; -             varSTRTOBYTES6 = System.Text.Encoding.UTF7.GetBytes (str6);
System.Text.Encoding.GetBytes (String Str)

second, byte arrays are converted into strings

1  //2. byte array converted to string2 3Console.Write ("StrToBytes1 is converted to a string using UTF8 encoding:");4             varByteToString1 =System.Text.Encoding.UTF8.GetString (strToBytes1);5 Console.WriteLine (byteToString1);6Console.Write ("StrToBytes1 is converted to a string using the default Encoding:");7             varByteToString2 =System.Text.Encoding.Default.GetString (strToBytes2);8 Console.WriteLine (byteToString2);9Console.Write ("StrToBytes1 is converted to a string using Unicode encoding:");Ten             varByteToString3 =System.Text.Encoding.Unicode.GetString (strToBytes3); one Console.WriteLine (byteToString3); aConsole.Write ("StrToBytes1 is converted to a string using ASCII encoding:"); -             varByteToString4 =System.Text.Encoding.ASCII.GetString (strToBytes4); - Console.WriteLine (byteToString4); theConsole.Write ("StrToBytes1 is converted to a string using UTF32 encoding:"); -             varByteToString5 =System.Text.Encoding.UTF32.GetString (strToBytes5); - Console.WriteLine (byteToString5); -Console.Write ("StrToBytes1 is converted to a string using UTF7 encoding:"); +             varByteToString6 =System.Text.Encoding.UTF7.GetString (strToBytes6); -Console.WriteLine (byteToString6);
System.Text.Encoding.GetString (byte[] Byte)

three, string conversion into a stream

1   // System.IO.MemoryStream 2             // 3. String conversion to stream 3             new  System.IO.MemoryStream (strToBytes2); 4             New System.IO.MemoryStream (convert.frombase64string (str1));
System.IO.MemoryStream

Iv. transfer to a string

1   // System.IO.MemoryStream 2             // 4. Transfer to string 3             var memoryToString1 = System.Text.Encoding.Default.GetString (ms2 . ToArray ()); 4             var memoryToString2 = convert.tobase64string (ms2. ToArray ());
System.IO.MemoryStream

V. Convert a byte array into a stream

1   // 5. byte array converted to stream 2             new  System.IO.MemoryStream (strToBytes1); 3 4             New 0, strtobytes1.length);
converting a byte array into a stream

six, Transfer to byte array

1 // 6. Transfer to byte array 23byte[] bt = ms3. ToArray (); 4 5 New System.IO.MemoryStream (); 6 0, (int) ms4. Length);
convert to byte array

C # strings and byte arrays are converted to each other

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.