byte[] bytes = new byte[] { 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100 }; string str; Decoder decoder = Encoding.UTF8.GetDecoder(); char[] chars = new char[decoder.GetCharCount(bytes, 0, bytes.Length)]; decoder.GetChars(bytes, 0, bytes.Length, chars, 0); str = new string(chars); Console.WriteLine(str); // Hello World
byte[] bytes = new byte[] { 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100 }; string str; Decoder decoder = Encoding.ASCII.GetDecoder(); char[] chars = new char[decoder.GetCharCount(bytes, 0, bytes.Length)]; decoder.GetChars(bytes, 0, bytes.Length, chars, 0); str = new string(chars); Console.WriteLine(str); // Hello WorldIn this example, a byte array is converted to a character string using GetDecoder and the ASCII encoding. The process is similar to the previous example but with a different encoding type. Package library: System.Text.Encoding Version: 4.0.0.0