Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Encoding strings to bytes and decoding bytes in Java

Choose an explicit charset to recover text from bytes and produce repeatable input for a hash. Learn which overloads replace invalid input and how to reject it instead.

Version: Java SE 21

Syntax

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

Charset charset = StandardCharsets.UTF_8;
byte[] bytes = text.getBytes(charset);       // String -> byte[]
String decoded = new String(bytes, charset); // byte[] -> String

The getBytes(Charset) overload of String.getBytes encodes text into a new byte[]. The String(byte[],Charset) constructor goes the other way: new String(bytes, charset) decodes the entire array into a new string. String.length() counts UTF-16 code units; bytes.length counts bytes.

Use the charset required by the format or protocol. For UTF-8, use StandardCharsets.UTF_8, a Charset constant in java.nio.charset that every Java implementation supports. getBytes() and new String(bytes) use the JVM's default charset: UTF-8 in Java SE 21 unless changed in an implementation-specific way. Overloads that take a charset name as a String throw UnsupportedEncodingException for unsupported names. The deprecated getBytes(int,int,byte[],int) just copies the low eight bits of each char; it is not a text encoder.

The overloads that take a Charset replace malformed or unmappable sequences. Encoding inserts the charset's default replacement bytes; decoding inserts its default replacement string. To reject both kinds of errors, use a CharsetEncoder or CharsetDecoder with CodingErrorAction.REPORT. When processing a whole buffer in one call, these errors throw CharacterCodingException. Successful decoding does not prove that you chose the right charset.

To check whether a round trip is lossless, encode and decode with the same charset and compare the decoded string with the original. Encoding without replacement is not enough: some charsets can encode a character to bytes that decode to a different character. SHA-256 hashes bytes, not text: agree on both the charset and the exact text form, including line endings and Unicode normalization. Do not decode digest bytes as text; display them in hexadecimal instead.

Examples

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

public class StringEncodingDemo {
    public static void main(String[] args) throws NoSuchAlgorithmException {
        Charset utf8 = StandardCharsets.UTF_8;
        String text = "x".repeat(39) + "\u00E9";
        byte[] utf8Bytes = text.getBytes(utf8);
        String decoded = new String(utf8Bytes, utf8);
        byte[] latin1Bytes = text.getBytes(StandardCharsets.ISO_8859_1);

        MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
        byte[] originalDigest = sha256.digest(utf8Bytes);
        byte[] decodedDigest = sha256.digest(decoded.getBytes(utf8));

        System.out.println("utf16Units=" + text.length());
        System.out.println("utf8Bytes=" + utf8Bytes.length);
        System.out.println("latin1Bytes=" + latin1Bytes.length);
        System.out.println("roundTrip=" + text.equals(decoded));
        System.out.println("sameSha256AfterRoundTrip=" + Arrays.equals(originalDigest, decodedDigest));
    }
}

Result

utf16Units=40
utf8Bytes=41
latin1Bytes=40
roundTrip=true
sameSha256AfterRoundTrip=true

Thirty-nine `x` characters and one `é` take 40 UTF-16 code units and 41 UTF-8 bytes. ISO-8859-1 uses 40 bytes for the same text. Decoding and re-encoding with UTF-8 restores the same bytes, so SHA-256 produces the same result.

Related lessons

  • Java · Lesson 62 · Advanced · Version: 2
  • Java · Lesson 63 · Advanced · Version: 2

Sources