core/src/Extensions/MiscExtensions.cs
/// <summary>
/// Converts a byte array to a string, using its byte order mark to convert it to the right encoding.
/// http://www.shrinkrays.net/code-snippets/csharp/an-extension-method-for-converting-a-byte-array-to-a-string.aspx
/// </summary>
/// <param name="buffer">An array of bytes to convert</param>
/// <returns>The byte as a string.</returns>
Converts a byte array to a string, using its byte order mark to convert it to the right encoding.
Converts a byte array to a string using its byte order mark to convert it to the right encoding .
public static string AsString(this byte[] buffer)
        {
            if (buffer == null) return "";
            Encoding encoding = Encoding.UTF8;
#if FRAMEWORK
            return encoding.GetString(buffer, 0, buffer.Length);
#else
            if (buffer == null || buffer.Length == 0)
                return "";
            if (buffer[0] == 0xef && buffer[1] == 0xbb && buffer[2] == 0xbf)
            {
                encoding = Encoding.UTF8;
            }
            else if (buffer[0] == 0xfe && buffer[1] == 0xff)
            {
                encoding = Encoding.Unicode;
            }
            else if (buffer[0] == 0xfe && buffer[1] == 0xff)
            {
                encoding = Encoding.BigEndianUnicode; 
            }
            using (MemoryStream stream = new MemoryStream())
            {
                stream.Write(buffer, 0, buffer.Length);
                stream.Seek(0, SeekOrigin.Begin);
                using (StreamReader reader = new StreamReader(stream, encoding))
                {
                    return reader.ReadToEnd();
                }
            }
#endif
        }
public static string AsString ( this byte [ ] buffer ) { if ( buffer == null ) return " " ; Encoding encoding = Encoding . UTF8 ; if FRAMEWORK return encoding . GetString ( buffer , 0 , buffer . Length ) ; else if ( buffer == null || buffer . Length == 0 ) return " " ; if ( buffer [ 0 ] == 0xef && buffer [ 1 ] == 0xbb && buffer [ 2 ] == 0xbf ) { encoding = Encoding . UTF8 ; } else if ( buffer [ 0 ] == 0xfe && buffer [ 1 ] == 0xff ) { encoding = Encoding . Unicode ; } else if ( buffer [ 0 ] == 0xfe && buffer [ 1 ] == 0xff ) { encoding = Encoding . BigEndianUnicode ; } using ( MemoryStream stream = new MemoryStream ( ) ) { stream . Write ( buffer , 0 , buffer . Length ) ; stream . Seek ( 0 , SeekOrigin . Begin ) ; using ( StreamReader reader = new StreamReader ( stream , encoding ) ) { return reader . ReadToEnd ( ) ; } } endif }
Converts a byte array to a string, using its byte order mark to convert it to the right encoding.
Converts a byte array to a string using its byte order mark to convert it to the right encoding .
// UTF8 as default /*
                EF BB BF		UTF-8 
                FF FE UTF-16	little endian 
                FE FF UTF-16	big endian 
                FF FE 00 00		UTF-32, little endian 
                00 00 FE FF		UTF-32, big-endian 
                */ // utf-16be
The byte as a string.
The byte as a string .
byte[]
this
buffer
An array of bytes to convert
An array of bytes to convert