problem
FCL's return value of many methods is a BYTE array containing characters rather than returns a string, such a method is included in the following classes:
· 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
The BYTE array contained in these methods is usually coded or Unicode encoded characters, many times, we may need to convert such a BYTE array into a string.
solution
Transforming a Byte array of ASCII coding characters into a complete string, you can use the following methods:
Using system;
Using system.text;
Public static string fromasciibytearray (byte [] characters)
{
ASCIENCODING Encoding = new asciiencoding ();
String constructedString = encoding.getstring (characters);
Return (constructedString);
}
Convert a Byte array containing Unicode coded characters to a complete string, you can use the following method:
Public Static String fromunicoreByTearray (byte [] character
{
UnicodeEncoding Encoding = new unicodeEncoding ();
String constructedString = encoding.getstring (characters);
Return (constructedString);
}
discuss
The getString method of the Asciiencoding class can convert the 7-bitsascii characters in the BYTE array into a string; any value greater than 127 will be converted to two characters. In System.Text Namespace You can find the Asciiencoding class, find the getString function of the class. You can also find that this function has multiple overloaded methods to support some additional parameters. The overload version of this method can also convert some of the characters in a BYTE array to String. The getString method that converts the Byte array into string can be found in the UnicodeEncoding class in the System.Text Namespace, which converts the BYTE array containing the 16-BITSUNICODE character to String. Like the getString method of the Asciiencoding class, the method also includes overloaded version of the specific part in the BYTE array into string.
reference
For more knowledge, please refer to the MSDN documentation