How to convert byte array to string
Categories:
Converting Byte Arrays to Strings in C#

Learn various methods to convert byte arrays into strings in C#, understanding character encodings, performance considerations, and best practices for handling binary data.
Converting a byte array to a string is a common task in C# development, especially when dealing with network communication, file I/O, or cryptographic operations. The key challenge lies in correctly interpreting the sequence of bytes as characters, which is heavily dependent on the character encoding used. This article will explore different approaches, focusing on the System.Text.Encoding
class, BitConverter
, and BinaryReader
, along with their appropriate use cases.
Understanding Character Encodings
Before diving into conversion methods, it's crucial to grasp the concept of character encodings. A character encoding defines how a sequence of bytes represents text characters. Without specifying the correct encoding, a byte array might be misinterpreted, leading to garbled or incorrect strings. Common encodings include UTF-8, UTF-16 (Unicode), ASCII, and ISO-8859-1.
- UTF-8: A variable-width encoding that is widely used on the internet. It's backward compatible with ASCII.
- UTF-16 (Unicode): A fixed-width encoding (usually 2 bytes per character for most common characters) often used internally by Windows and .NET.
- ASCII: A 7-bit encoding that represents 128 characters, primarily English letters, numbers, and symbols.
- ISO-8859-1 (Latin-1): An 8-bit encoding that extends ASCII to include characters from Western European languages.
flowchart TD A[Byte Array] --> B{Choose Encoding} B -->|UTF-8| C[UTF8 Encoding] B -->|UTF-16| D[Unicode Encoding] B -->|ASCII| E[ASCII Encoding] C --> F[String Output] D --> F E --> F F[String Output] --> G{Correct Interpretation?} G -->|Yes| H[Success] G -->|No| I[Garbled Text (Encoding Mismatch)]
Flowchart of Byte Array to String Conversion with Encoding Selection
Using System.Text.Encoding for Text Data
The System.Text.Encoding
class is the primary and most robust way to convert byte arrays to strings when dealing with textual data. It provides static properties for common encodings like UTF8
, Unicode
(UTF-16), and ASCII
. You can also get other encodings using Encoding.GetEncoding()
.
The GetString()
method of an Encoding
object takes a byte array and returns the corresponding string. It's vital to use the same encoding for decoding (byte array to string) as was used for encoding (string to byte array).
using System;
using System.Text;
public class ByteArrayToStringConverter
{
public static void Main(string[] args)
{
// Original string
string originalString = "Hello, World! This is a test with some special characters: éàç";
// --- UTF-8 Encoding ---
byte[] utf8Bytes = Encoding.UTF8.GetBytes(originalString);
string utf8String = Encoding.UTF8.GetString(utf8Bytes);
Console.WriteLine($"UTF-8 Original: {originalString}");
Console.WriteLine($"UTF-8 Converted: {utf8String}");
Console.WriteLine($"UTF-8 Bytes Length: {utf8Bytes.Length}\n");
// --- Unicode (UTF-16) Encoding ---
byte[] unicodeBytes = Encoding.Unicode.GetBytes(originalString);
string unicodeString = Encoding.Unicode.GetString(unicodeBytes);
Console.WriteLine($"Unicode Original: {originalString}");
Console.WriteLine($"Unicode Converted: {unicodeString}");
Console.WriteLine($"Unicode Bytes Length: {unicodeBytes.Length}\n");
// --- ASCII Encoding (Lossy for special characters) ---
byte[] asciiBytes = Encoding.ASCII.GetBytes(originalString);
string asciiString = Encoding.ASCII.GetString(asciiBytes);
Console.WriteLine($"ASCII Original: {originalString}");
Console.WriteLine($"ASCII Converted: {asciiString}"); // Note: Special characters will be replaced with '?'
Console.WriteLine($"ASCII Bytes Length: {asciiBytes.Length}\n");
// --- ISO-8859-1 Encoding ---
Encoding iso88591 = Encoding.GetEncoding("ISO-8859-1");
byte[] isoBytes = iso88591.GetBytes(originalString);
string isoString = iso88591.GetString(isoBytes);
Console.WriteLine($"ISO-8859-1 Original: {originalString}");
Console.WriteLine($"ISO-8859-1 Converted: {isoString}");
Console.WriteLine($"ISO-8859-1 Bytes Length: {isoBytes.Length}\n");
// Example of encoding mismatch
string mismatchString = Encoding.ASCII.GetString(utf8Bytes);
Console.WriteLine($"Encoding Mismatch (UTF-8 bytes decoded as ASCII): {mismatchString}");
}
}
Encoding.UTF8
for general-purpose text handling unless you have a specific reason and guarantee that another encoding is being used. UTF-8 is flexible, widely supported, and handles a vast range of characters.Converting Binary Data to Hexadecimal String Representation
Sometimes, you don't want to interpret a byte array as text, but rather represent its raw binary content as a human-readable hexadecimal string. This is common for debugging, logging, or displaying hashes and GUIDs. The BitConverter.ToString()
method is perfect for this purpose.
using System;
using System.Text;
public class BinaryToHexStringConverter
{
public static void Main(string[] args)
{
byte[] data = { 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x21 }; // "Hello World!"
// Using BitConverter.ToString()
string hexString = BitConverter.ToString(data);
Console.WriteLine($"Hexadecimal representation (BitConverter): {hexString}");
// Output: 48-65-6C-6C-6F-20-57-6F-72-6C-64-21
// Removing hyphens for a continuous string
string continuousHexString = hexString.Replace("-", string.Empty);
Console.WriteLine($"Continuous Hexadecimal: {continuousHexString}\n");
// Manual conversion for more control (e.g., lowercase, no hyphens)
StringBuilder sb = new StringBuilder();
foreach (byte b in data)
{
sb.Append(b.ToString("x2")); // "x2" formats as two lowercase hexadecimal digits
}
string manualHexString = sb.ToString();
Console.WriteLine($"Manual Hexadecimal: {manualHexString}");
}
}
Reading Strings from Binary Streams with BinaryReader
When working with binary files or network streams that contain structured data, including strings, BinaryReader
can be very useful. It allows you to read primitive data types, including strings, from a stream in a specific encoding. The ReadString()
method reads a string that has been written using BinaryWriter.Write(string)
(which prefixes the string with its length as a 7-bit encoded integer).
using System;
using System.IO;
using System.Text;
public class BinaryReaderStringExample
{
public static void Main(string[] args)
{
// Simulate writing data to a memory stream
using (MemoryStream ms = new MemoryStream())
{
using (BinaryWriter bw = new BinaryWriter(ms, Encoding.UTF8, true))
{
bw.Write(123);
bw.Write("Hello from BinaryWriter!");
bw.Write(true);
}
// Reset stream position to beginning for reading
ms.Position = 0;
// Now read the data using BinaryReader
using (BinaryReader br = new BinaryReader(ms, Encoding.UTF8, true))
{
int number = br.ReadInt32();
string message = br.ReadString(); // Reads length-prefixed string
bool booleanValue = br.ReadBoolean();
Console.WriteLine($"Read Number: {number}");
Console.WriteLine($"Read Message: {message}");
Console.WriteLine($"Read Boolean: {booleanValue}");
}
}
}
}
BinaryReader.ReadString()
method expects the string to be prefixed with its length. If the byte array or stream was not created using BinaryWriter.Write(string)
, ReadString()
will likely fail or return incorrect data. For raw byte arrays, Encoding.GetString()
is generally the correct choice.Performance Considerations
For most applications, the performance difference between Encoding.GetString()
and other methods is negligible. However, in high-performance scenarios or when processing very large byte arrays, consider the following:
Encoding.GetString()
: Generally efficient. The .NET runtime optimizes common encodings. Creating newEncoding
objects repeatedly can have a minor overhead; reuse instances if possible.BitConverter.ToString()
: Involves string concatenation and potentiallyReplace()
operations, which can be less performant for extremely large arrays compared to manualStringBuilder
approaches if hyphens need to be removed.BinaryReader.ReadString()
: Performance is tied to stream I/O and the underlying encoding. It's designed for structured binary data, not raw byte array conversion.
1. Identify the Original Encoding
Determine the character encoding that was used to convert the string into the byte array. This is the most critical step.
2. Select the Appropriate Method
If converting text, use System.Text.Encoding.GetString()
. If representing raw binary data as hexadecimal, use BitConverter.ToString()
or a manual StringBuilder
loop. If reading from a structured binary stream, use BinaryReader.ReadString()
.
3. Apply the Conversion
Execute the chosen method with the correct encoding (if applicable) and the byte array.
4. Verify the Output
Check the resulting string to ensure it's correctly interpreted and free of garbled characters. If not, re-evaluate the encoding used.