byte[] bytes = { 72, 101, 108, 108, 111 }; // ASCII encoding for "Hello" string str = Encoding.UTF8.GetString(bytes); // Convert bytes to string Console.WriteLine(str); // Output: "Hello"
string str = "Hello"; byte[] bytes = Encoding.ASCII.GetBytes(str); // Convert string to bytes foreach (byte b in bytes) { Console.Write(b + " "); // Output: 72 101 108 108 111 }
byte[] bytes = { 0x41, 0x00, 0x62, 0x00, 0x63, 0x00 }; // Unicode encoding for "Abc" string str = Encoding.Unicode.GetString(bytes); // Convert bytes to string Console.WriteLine(str); // Output: "Abc"Package Library: The System.Text Encoding class is included in the .NET Standard library, which provides a common set of APIs for all .NET implementations.