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

后端 未结 15 2508
迷失自我
迷失自我 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: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

提交回复
热议问题