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

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

    Try this console app:

    static void Main(string[] args)
    {
        //Encoding _UTF8 = Encoding.UTF8;
        string[] _mainString = { "Héllo World" };
        Console.WriteLine("Main String: " + _mainString);
    
        //Convert a string to utf-8 bytes.
        byte[] _utf8Bytes = Encoding.UTF8.GetBytes(_mainString[0]);
    
        //Convert utf-8 bytes to a string.
        string _stringuUnicode = Encoding.UTF8.GetString(_utf8Bytes);
        Console.WriteLine("String Unicode: " + _stringuUnicode);
    }
    

提交回复
热议问题