Recipe2.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.Diagnostics.EventLog.WriteEntry System.IO.BinaryWriter.Write System.IO.FileStream.Write System.IO.FileStream.BeginWrite System.IO.MemoryStream.Write System.IO.MemoryStream.BeginWrite 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.Security.Cryptography.CryptoStream.Write System.Security.Cryptography.CryptoStream.BeginWrite 

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

Solution

To convert a string to a byte[] of ASCII values, use the GetBytes method on the Encoding class:

 byte[] retArray = Encoding.ASCII.GetBytes(characters); 

To convert a string to a byte[] of Unicode values, use the GetBytes method on the Encoding class:

 byte[] retArray = Encoding.Unicode.GetBytes(characters); 

Discussion

The GetBytes method of the Encoding class (returned by the ASCII property) converts ASCII characterscontained in either a char[] or a stringinto a byte[] of 7-bit ASCII values. Any value larger than 127 (0x7F) is converted to the ? character. The Encoding 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[], which is returned to the caller.

The GetBytes method of the Encoding class (returned by the Unicode property) converts Unicode characters into 16-bit Unicode values. The Encoding class can be found in the System.Text namespace. The GetBytes method returns a byte[], 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[] corresponds to two elements of the byte[]. For example, the following byte[] contains the ASCII value of the letter S:

 byte[] sourceArray = {83}; 

However, for a byte[] to contain a Unicode representation 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. This is accomplished by using one of the two following constructors:

 public UnicodeEncoding (bool bigEndian, bool byteOrderMark); public UnicodeEncoding (bool bigEndian, bool byteOrderMark,                         bool throwOnInvalidBytes); 

The first parameter, bigEndian, accepts a Boolean argument. Set this argument to true to use big-endian or false to use little-endian.

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
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

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