Programming
Byte Array
String Conversion
Coding Tutorials
Duplicate Content

How to convert byte array to string

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Converting a byte array to a string is not just a matter of calling the right method. The critical question is which character encoding the bytes represent, because the exact same byte sequence can decode into different text depending on whether you treat it as UTF-8, UTF-16, ASCII, or something else.

Why Encoding Matters

A byte array is raw binary data. A string is text interpreted through an encoding. If you decode with the wrong encoding, the code may run successfully and still produce corrupted text.

For example, these bytes represent "hello" in UTF-8:

python
data = b"hello"
print(data.decode("utf-8"))

That works because the byte sequence and the decoding agree on the same character set.

Python Example

In Python, bytes objects use .decode(...).

python
1data = "café".encode("utf-8")
2text = data.decode("utf-8")
3
4print(data)
5print(text)

You can also handle bad input explicitly:

python
data = b"\xff"
print(data.decode("utf-8", errors="replace"))

Using errors="replace" or errors="ignore" can be useful when dealing with untrusted or partially corrupted input, though replacing or ignoring bytes also loses information.

Java Example

In Java, always pass a charset rather than relying on the platform default.

java
1import java.nio.charset.StandardCharsets;
2
3public class ByteArrayToStringDemo {
4    public static void main(String[] args) {
5        byte[] data = "café".getBytes(StandardCharsets.UTF_8);
6        String text = new String(data, StandardCharsets.UTF_8);
7
8        System.out.println(text);
9    }
10}

This avoids a whole class of bugs where text behaves differently across operating systems or JVM configurations.

C# Example

In C#, the Encoding class performs the conversion.

csharp
1using System;
2using System.Text;
3
4public class Program
5{
6    public static void Main()
7    {
8        byte[] data = Encoding.UTF8.GetBytes("café");
9        string text = Encoding.UTF8.GetString(data);
10
11        Console.WriteLine(text);
12    }
13}

Again, the important part is making the encoding explicit.

What if the Bytes Are Not Text

Sometimes a byte array is not meant to be decoded as text at all. It may represent:

  • compressed data
  • an image
  • encrypted bytes
  • a hash or checksum

In those cases, converting directly to a string is usually the wrong operation. A safer textual representation might be hexadecimal or Base64.

For example, in Python:

python
1import base64
2
3data = b"\x01\x02\xff"
4print(base64.b64encode(data).decode("ascii"))

That gives you a stable textual encoding of arbitrary binary data without pretending it is normal human-readable text.

Dealing With Null Bytes and Partial Buffers

If bytes come from a socket or file buffer, the array may contain unused trailing bytes or embedded null bytes. Decode only the meaningful slice.

In Java:

java
1import java.nio.charset.StandardCharsets;
2import java.util.Arrays;
3
4byte[] buffer = new byte[8];
5byte[] source = "hi".getBytes(StandardCharsets.UTF_8);
6System.arraycopy(source, 0, buffer, 0, source.length);
7
8String text = new String(Arrays.copyOf(buffer, 2), StandardCharsets.UTF_8);
9System.out.println(text);

This avoids accidentally decoding uninitialized or padding bytes.

Common Pitfalls

The biggest mistake is relying on a platform default charset. That makes behavior environment-dependent and hard to debug.

Another issue is assuming every byte array is text. Many byte arrays are just binary data and should be represented as Base64 or hex instead of decoded as characters.

Developers also ignore decoding errors and end up with silent data corruption. If bytes may be invalid, decide deliberately whether to fail, replace, or skip bad sequences.

Finally, be careful with partial buffers from streams and network reads. Decode only the bytes you actually received, not the full allocated buffer by default.

Summary

  • Byte-array-to-string conversion only makes sense when you know the correct character encoding.
  • Always specify the encoding explicitly instead of relying on defaults.
  • Use language-native decoding APIs such as Python .decode, Java new String(bytes, charset), or C# Encoding.UTF8.GetString.
  • If the bytes are arbitrary binary data, use Base64 or hex instead of pretending they are text.
  • Decode only meaningful buffer slices when working with streamed or partially filled arrays.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.