How to convert UTF-8 byte[] to string?

后端 未结 15 2329
迷失自我
迷失自我 2020-11-22 03:11

I have a byte[] array that is loaded from a file that I happen to known contains UTF-8.

In some debugging code, I need to convert it to a string. Is

相关标签:
15条回答
  • 2020-11-22 03:58

    Using (byte)b.ToString("x2"), Outputs b4b5dfe475e58b67

    public static class Ext {
    
        public static string ToHexString(this byte[] hex)
        {
            if (hex == null) return null;
            if (hex.Length == 0) return string.Empty;
    
            var s = new StringBuilder();
            foreach (byte b in hex) {
                s.Append(b.ToString("x2"));
            }
            return s.ToString();
        }
    
        public static byte[] ToHexBytes(this string hex)
        {
            if (hex == null) return null;
            if (hex.Length == 0) return new byte[0];
    
            int l = hex.Length / 2;
            var b = new byte[l];
            for (int i = 0; i < l; ++i) {
                b[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
            }
            return b;
        }
    
        public static bool EqualsTo(this byte[] bytes, byte[] bytesToCompare)
        {
            if (bytes == null && bytesToCompare == null) return true; // ?
            if (bytes == null || bytesToCompare == null) return false;
            if (object.ReferenceEquals(bytes, bytesToCompare)) return true;
    
            if (bytes.Length != bytesToCompare.Length) return false;
    
            for (int i = 0; i < bytes.Length; ++i) {
                if (bytes[i] != bytesToCompare[i]) return false;
            }
            return true;
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 03:58

    There is also class UnicodeEncoding, quite simple in usage:

    ByteConverter = new UnicodeEncoding();
    string stringDataForEncoding = "My Secret Data!";
    byte[] dataEncoded = ByteConverter.GetBytes(stringDataForEncoding);
    
    Console.WriteLine("Data after decoding: {0}", ByteConverter.GetString(dataEncoded));
    
    0 讨论(0)
  • 2020-11-22 03:59

    To my knowledge none of the given answers guarantee correct behavior with null termination. Until someone shows me differently I wrote my own static class for handling this with the following methods:

    // Mimics the functionality of strlen() in c/c++
    // Needed because niether StringBuilder or Encoding.*.GetString() handle \0 well
    static int StringLength(byte[] buffer, int startIndex = 0)
    {
        int strlen = 0;
        while
        (
            (startIndex + strlen + 1) < buffer.Length // Make sure incrementing won't break any bounds
            && buffer[startIndex + strlen] != 0       // The typical null terimation check
        )
        {
            ++strlen;
        }
        return strlen;
    }
    
    // This is messy, but I haven't found a built-in way in c# that guarentees null termination
    public static string ParseBytes(byte[] buffer, out int strlen, int startIndex = 0)
    {
        strlen = StringLength(buffer, startIndex);
        byte[] c_str = new byte[strlen];
        Array.Copy(buffer, startIndex, c_str, 0, strlen);
        return Encoding.UTF8.GetString(c_str);
    }
    

    The reason for the startIndex was in the example I was working on specifically I needed to parse a byte[] as an array of null terminated strings. It can be safely ignored in the simple case

    0 讨论(0)
提交回复
热议问题