Convert Byte arrays to String

xiaoxiao2021-03-06  124

Problem FCL Many return values ​​of many methods are all BYTE arrays containing characters rather than return a string, such methods are 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 as ASCII or Unicode characters encoded Byte array returned by these methods included, many times We can It is necessary 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.

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

New Post(0)