Cooking with C#
by Stephen Teilhet and Jay Hilyard|
Related Reading
C# Cookbook |
Editor's note: Converting strings and handling exceptions, two of the topics covered in depth in today's recipes, are just a few of the hundreds of solutions to specific problems included in C# Cookbook. Whether you're new to the C# language or an experienced C# programmer, C# Cookbook provides practical answers to the day-to-day questions you run up against.
Recipe 2.13: Converting a String Returned as a Byte[ ] Back into a String
Problem
Many methods in the FCL return a byte[] consisting of characters instead of a
string. Some of these methods include:
- System.Net.Sockets.Socket.Receive
- System.Net.Sockets.Socket.ReceiveFrom
- System.Net.Sockets.Socket.BeginReceive
- System.Net.Sockets.Socket.BeginReceiveFrom
- System.Net.Sockets.NetworkStream.Read
- System.Net.Sockets.NetworkStream.BeginRead
- System.IO.BinaryReader.Read
- System.IO.BinaryReader.ReadBytes
- System.IO.FileStream.Read
- System.IO.FileStream.BeginRead
- System.IO.MemoryStream // Constructor
- System.IO.MemoryStream.Read
- System.IO.MemoryStream.BeginRead
- System.Security.Cryptography.CryptoStream.Read
- System.Security.Cryptography.CryptoStream.BeginRead
- System.Diagnostics.EventLogEntry.Data
In many cases, this byte[] might contain ASCII or
Unicode encoded characters. You need a way to recombine this byte[]
to obtain the original string.
Solution
To convert a byte array of ASCII
values to a complete string, use the following method:
using System;
using System.Text;
public static string FromASCIIByteArray(byte[] characters)
{
ASCIIEncoding encoding = new ASCIIEncoding( );
string constructedString = encoding.GetString(characters);
return (constructedString);
}
To convert a byte array of Unicode values
(UTF-16 encoded) to a complete string, use the following method:
public static string FromUnicodeByteArray(byte[] characters)
{
UnicodeEncoding encoding = new UnicodeEncoding( );
string constructedString = encoding.GetString(characters);
return (constructedString);
}
Discussion
The GetString method of the
ASCIIEncoding class converts 7-bit ASCII characters contained in a
byte array to a string. Any value larger than 127 will be AND'ed with the value 127 and the
resulting character value will be displayed in the string. For
example, if
the byte array contains the value 200, this value will be converted to
72,
and the character equivalent of 72 ('H') will be displayed.
The ASCIIEncoding class
can be found in the System.Text namespace. The
GetString 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.
The GetString method returns a string containing
the converted byte array of ASCII characters.
The GetString 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 GetString method returns a
string containing the converted byte array of Unicode
characters.
See Also
See the "ASCIIEncoding Class" and "UnicodeEncoding Class" topics in the MSDN documentation.
Pages: 1, 2 |

