C # string byte[] Base64 commonly used to convert each other

Source: Internet
Author: User

Reference:

Http://www.cnblogs.com/zxx193/p/3605238.html?utm_source=tuicool

Http://www.cnblogs.com/freeliver54/p/3430956.html

Http://www.cnblogs.com/simhare/archive/2007/07/18/821938.html

Defines the string variable as str, the memory rheology is MS, and the bit array is BT

1. String = = array of bits

(1) byte[] Bt=system.text.encoding.default.getbytes ("string");(2) byte[] bt=convert.frombase64string ("string");

Add:

System.Text.Encoding.Unicode.GetBytes (str); System.Text.Encoding.UTF8.GetBytes (str); System.Text.Encoding.GetEncoding ("gb2312"). GetBytes (str); Specify the Encoding method
String str = "China?" Ss123? "; byte[] bytes = System.Text.Encoding.Default.GetBytes (str); GB2312 encoded Chinese characters accounted for 2 bytes, English letters accounted for 1 bytes bytes length 12string s = System.Text.Encoding.Default.GetString (new byte[] {bytes[0],bytes[1 ]});//decoded to "medium"
byte[] bytes = {98, 101, 102};  String str = System.Text.Encoding.ASCII.GetString (bytes); The result is: ABCDEF  ASCII Code table

2. Bit array = string

(1) string str=system.text.encoding.default.getstring (BT);(2) string str=convert.tobase64string (BT);

3. String = Flow

(1) MemoryStream ms=new MemoryStream (System.Text.Encoding.Default.GetBytes ("string"));(2) MemoryStream ms=new MemoryStream (Convert.frombase64string ("string"));

4. Stream = = string

(1) String str=convert.tobase64string (Ms. ToArray ());(2) string str=system.text.encoding.default.getstring (Ms. ToArray ());

5. Array of bits = flow

(1) MemoryStream ms=new MemoryStream (BT);(2) MemoryStream ms=new MemoryStream (); Ms. Read (BT,0,BT. Lenght);

6. Stream = array of bits

(1) byte[] Bt=ms. ToArray ();(2) MemoryStream ms=new MemoryStream (), Ms. Write (Bt,0,ms. Length);

7, byte[] and base64string of the mutual conversion

in C # picture to byte[] to base64string conversion: Bitmap BMP=NewBitmap (filepath); MemoryStream Ms=NewMemoryStream (); Bmp.                 Save (MS, SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.GIF); byte[] arr =New byte[Ms.                 Length]; Ms. Position=0; Ms. Read (arr,0, (int) Ms.                 Length); Ms.             Close (); stringPic =convert.tobase64string (arr); Base64string to byte[] again to the conversion of the picture:byte[] Imagebytes =convert.frombase64string (pic); //read in MemoryStream objectMemoryStream MemoryStream =NewMemoryStream (Imagebytes,0, imagebytes.length); Memorystream.write (Imagebytes,0, imagebytes.length); //turn into a pictureImage image =Image.fromstream (MemoryStream); Now in the database development: The picture is generally stored in a CLOB: storage base64string BLOB: storage byte [] It is generally recommended to use byte[]. Because the picture can be converted directly to byte[] into the database, if you use base64string you also need to convert from byte[] to base64string. More wasted performance. 
View Code

Conversion of C # byte array to image

Http://www.cnblogs.com/luxiaoxun/p/3378416.html

C # string byte[] Base64 commonly used to convert 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.