January 2004
Beginner to intermediate
864 pages
22h 18m
English
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.
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 = ...