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 );