[Repost] Transform the Byte array to String

xiaoxiao2021-03-06  41

Article Source from Johnny's column address: http://blog.9cbs.net/gztoby/archive/2004/08.ASPXFCL The return value of many methods is all BYTE arrays that contain characters rather than return a string, such a method contains 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 is typically included in the BYTE array returned by these methods, which is usually encoded in ASCII or Unicode, many times, we may need to convert such a BYTE array into a string. The solution comprises a Byte array ASCII coded characters into a full String, you can 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);} byte array containing a Unicode character encoding for a complete conversion of String, can use the following methods: public static string FromUnicodeByteArray (byte [] characters) { UnicodeEncoding encoding = new UnicodeEncoding (); string constructedString = encoding.GetString (characters); return (constructedString);} GetString method discussed can be ASCIIEncoding class byte array 7-BitsASCII character 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.

转载请注明原文地址:https://www.9cbs.com/read-67318.html

New Post(0)