Recipe 2.14 Passing a String to a Method that Accepts Only a Byte

Recipe 2.14 Passing a String to a Method that Accepts Only a Byte[ ]

Problem

Many methods in the FCL accept a byte[] consisting of characters instead of a string . Some of these methods include:

 System.Net.Sockets.Socket.Send System.Net.Sockets.Socket.SendTo System.Net.Sockets.Socket.BeginSend System.Net.Sockets.Socket.BeginSendTo System.Net.Sockets.NetworkStream.Write System.Net.Sockets.NetworkStream.BeginWrite System.IO.BinaryWriter.Write System.IO.FileStream.Write System.IO.FileStream.BeginWrite System.IO.MemoryStream.Write System.IO.MemoryStream.BeginWrite System.Security.Cryptography.CryptoStream.Write System.Security.Cryptography.CryptoStream.BeginWrite System.Diagnostics.EventLog.WriteEntry 

In many cases, you might have a string that you need to pass into one of these methods or some other method that only accepts a byte[] . You need a way to break up this string into a byte[] .

Solution

To convert a string to a byte array of ASCII values, use the GetBytes method on an instance of the ASCIIEncoding class:

 using System; using System.Text; public static byte[] ToASCIIByteArray(string characters) {     ASCIIEncoding encoding = new ASCIIEncoding( );     int numberOfChars = encoding.GetByteCount(characters);     byte[] retArray = new byte[numberOfChars];     retArray = encoding.GetBytes(characters);     return (retArray); } 

To convert a string to a byte array of Unicode values, use the UnicodeEncoding class:

 public static byte[] ToUnicodeByteArray(string characters) {     UnicodeEncoding encoding = new UnicodeEncoding( );     int numberOfChars = encoding.GetByteCount(characters);     byte[] retArray = new byte[numberOfChars];     retArray = encoding.GetBytes(characters);     return (retArray); } 

Discussion

The GetBytes method of the ASCIIEncoding class converts ASCII characterscontained in either a char array or a string into a byte array of 7-bit ASCII values. Any value larger than 127 is converted to the ? character. The ASCIIEncoding class can be found in the System.Text namespace. The GetBytes method is overloaded to accept additional arguments as well. The overloaded versions of the method convert all or part of a string to ASCII and then store the result in a specified range inside a byte array, which is returned to the caller.

The GetBytes method of the UnicodeEncoding class converts Unicode characters into 16-bit Unicode values. The UnicodeEncoding class can be found in the System.Text namespace. The GetBytes method returns a byte array, each element of which contains the Unicode value of a single character of the string.

A single Unicode character in the source string or in the source char array corresponds to two elements of the byte array. For example, the following byte array contains the ASCII value of the letter ' S ':

 byte[] sourceArray = {83}; 

However, for a byte array to contain a Unicode representation (UTF-16 encoded) of the letter ' S ', it must contain two elements. For example:

 byte[] sourceArray = {83, 0}; 

The Intel architecture uses a little-endian encoding, which means that the first element is the least-significant byte and the second element is the most-significant byte. Other architectures may use big-endian encoding, which is the opposite of little-endian encoding. The UnicodeEncoding class supports both big-endian and little-endian encodings. Using the UnicodeEncoding instance constructor, you can construct an instance that uses either big-endian or little-endian ordering. In addition, you have the option to indicate whether a byte order mark preamble should be generated so that readers of the file will know which endianness is in use.

See Also

See the "ASCIIEncoding Class" and "UnicodeEncoding Class" topics in the MSDN documentation .



C# Cookbook
C# 3.0 Cookbook
ISBN: 059651610X
EAN: 2147483647
Year: 2003
Pages: 315

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net