C # DataTable conversion byte

Source: Internet
Author: User

Converting a able into a byte [] and converting a byte [] into a datatable is actually a serialization and inverse series problem. in fact, all classes can be expressed in byte [] format, because the data in the memory itself is byte

For the string you are talking about

System. Text. encoding. bigendianunicode. getbytes

System. Text. encoding. Default. getbytes, etc.

In addition to the encoding reading function, there is no way to encode the text. Each country has its own standard to occupy less space for its own text storage, it is difficult to support texts in other countries. Of course, it also supports UTF-8 of texts in other countries.

Method 1:

For more information, see run
Reference namespace
Using system. IO;
Using system. runtime. serialization. formatters. Binary;

Code segment:

Datatable dt = new datatable (); // converts data to an instance of byte []
DT. Columns. Add ("");
DT. Rows. Add ("B"); // Add a test data B.
System. Io. memorystream memory = new memorystream (); // use the memory stream to store these bytes []
Binaryformatter B = new binaryformatter ();
B. Serialize (memory, dt); // serialized datatable. MS has implemented a serialized interface for datatable. If your custom classes need to be serialized, IFormatter can be implemented in a similar way.
Byte [] buff = memory. GetBuffer (); // you can use this to transmit the desired byte [].
Memory. Close ();

// If the received byte [] buff is still received, this will be used for anti-serialization.

DataTable dt1 = (DataTable) B. Deserialize (new MemoryStream (buff); // dt1 is the able returned by byte []
Response. Write (dt1.Rows [0] [0]. ToString ());

// The output is "B"

Method 2

MemoryStream memory = new MemoryStream ();
System. Xml. XmlTextWriter xtw = new System. Xml. XmlTextWriter (memory, System. Text. Encoding. UTF8 );

Dt. WriteXml (memory );
Int count = (int) memory. Length;
Byte [] temp = new byte [count];
Memory. Seek (0, SeekOrigin. Begin );
Memory. Read (temp, 0, count );
String returnValue = Convert. ToBase64String (temp );
Return returnValue;

Read string Information
Byte [] bstr = Convert. FromBase64String (strType );
System. Text. Encoding ed = System. Text. Encoding. UTF8;
String returnValue = ed. GetString (bstr, 0, bstr. Length). Trim ();

// Converts a byte to a able

Memorystream MS = new memorystream (BSTR );
System. xml. xmltextreader xtw = new system. xml. xmltextreader (MS );
DS. readxml (xtw );

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.