Complete-Reference-Vb Net 61
Complete-Reference-Vb Net 61
ToArray
The ToArray method is useful for translocating the contents of the MemoryStream to a formal Byte array. If
the current object was instantiated on a Byte array, then a copy of the section of the array to which this
instance has access is returned. MemoryStream also supports a WriteTo method that lets you write the entire
contents of the memory stream to another streamone that has a persistent backing store, for example.
So far we have looked at classes that let you work with Strings that also provide a facility for you to retrieve
or supply the String. We have also gone from manipulating String data to constructing Strings and using
them in various display fields. While capturing the values provided by the various ToString methods is
possible, the classes and utilities discussed earlier don't provide much in the way of features that get data on
the road. StringReader and StringWriter provide the basic facilities for character I/O.
The .NET Framework's data streaming (I/O) support inherits from the abstract TextReader and TextWriter
classes that live in the System.IO namespace. These classes form the basis of support for internationally
viable and highly distributed software because they support Unicode character streams.
Text Encoding
Before we look at the reader and writer classes, understand that methods are provided to convert Arrays and
Strings of Unicode characters to and from Arrays of Bytes encoded for a target code page. A number of
encoding implementations are thus provided in the System.Text namespace. The following list presents these
encoding classes:
• ASCIIEncoding class Encodes Unicode characters as single 7−bit ASCII characters. It only supports
character values between U+0000 and U+007F.
• UnicodeEncoding Encodes each Unicode character as two consecutive Bytes. Both little−endian
(code page 1200) and big−endian (code page 1201) Byte orders are supported.
• UTF7Encoding Encodes Unicode characters using the UTF−7 encoding (UTF−7 stands for UCS
Transformation Format, 7−bit form). This encoding supports all Unicode character values, and can
also be accessed as code page 65000.
• UTF8Encoding Encodes Unicode characters using the UTF−8 encoding (UTF−8 stands for UCS
Transformation Format, 8−bit form). This encoding supports all Unicode character values, and can
also be accessed as code page 65001.
Other encoding can be accessed using the GetEncoding method that passes a code page or name argument.
When the data to be converted is only available in sequential blocks (such as data read from a stream), an
application can use a decoder or an encoder to perform the conversion. This is also useful when the amount of
data is so large that it needs to be divided into smaller blocks. Decoders and encoders are obtained using the
GetDecoder and GetEncoder methods. An application can use the properties of this class, such as ASCII,
Default, Unicode, UTF7, and UTF8, to obtain encodings. Applications can initialize new instances of
encoding objects through the ASCIIEncoding, UnicodeEncoding, UTF7Encoding, and UTF8Encoding
classes.
Through an encoding, the GetBytes method is used to convert arrays of Unicode characters to Arrays of
Bytes, and the GetChars method is used to convert Arrays of Bytes to Arrays of Unicode characters. The
545