Converts a byte array to a string

Source: Internet
Author: User
Tags array to string socket
Array problem

FCL Many methods return values are byte arrays that contain characters instead of returning a string, and such methods are included in the following class:

· 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 returned by these methods contains usually ASCII-encoded or Unicode-encoded characters, and many times we may need to convert such a byte array to a string.

Solution

Converts a byte array that contains ASCII-encoded characters into a complete string, using the following methods:

Using System;
Using System.Text;

public static string Fromasciibytearray (byte[] characters)
{
ASCIIEncoding encoding = new ASCIIEncoding ();
String constructedstring = Encoding. GetString (characters);
return (constructedstring);
}


Converts a byte array that contains Unicode encoded characters to a complete string, using the following methods:

public static string Fromunicodebytearray (byte[] characters)
{
UnicodeEncoding encoding = new UnicodeEncoding ();
String constructedstring = Encoding. GetString (characters);
return (constructedstring);
}




Discuss

The GetString method of the ASCIIEncoding class converts the 7-bitsascii character in a byte array to a string; Any value greater than 127 will be converted to two characters. In the System.Text namespace you can find the ASCIIEncoding class and find the GetString function of the class you can also find that the function has several overloads to support some additional parameters. An overloaded version of this method can also convert a subset of characters from a byte array to string.

The GetString method of converting a byte array to string can be found in the UnicodeEncoding class in the System.Text namespace, which converts a byte array containing 16-bitsunicode characters to a string. Like the GetString method of the ASCIIEncoding class, this method also contains an overloaded version that converts a particular part of a byte array to string.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.