Java Character reverseBytes() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.lang.Character

Definition

In Java, Character.reverseBytes(ch) returns a char whose two bytes are swapped: the high byte becomes the low byte and vice versa. 0x1234 becomes 0x3412, and 'A' (0x0041) becomes 0x4100.

A char is 16 bits, stored as two bytes. Some systems write the high byte first (big-endian), others the low byte first (little-endian). When char data was read with the wrong byte order, reverseBytes fixes it.

A static method of java.lang.Character (Java 5+): call it on the class, as in Character.reverseBytes(...); you do not need a Character object. No import is needed, because java.lang is always available.

Syntax

Pass the char to swap.

Java
char a = Character.reverseBytes((char) 0x1234);   // 0x3412
char b = Character.reverseBytes('A');              // 0x4100 (16640)
char c = Character.reverseBytes(b);                // 'A' again
CallWhat it does
Character.reverseBytes(c)Swap the two bytes.
Character.reverseBytes(Character.reverseBytes(c)) == cSwapping twice restores the value.
(char) ((c << 8) | (c >>> 8))The same swap with shifts.

One overload, reverseBytes(char ch), since Java 5. Short, Integer, and Long have similar methods for their sizes.

Library declarations
Java
// java.lang.Character (since Java 5)
public static char reverseBytes(char ch)

Parameters

ParameterDescription
ch (char)The value whose bytes are swapped.

Return Value

Return ValueDescription
charThe value with its high and low bytes exchanged.

Compare with Similar Methods

Related tools:

MethodHow it differs
ByteBuffer.order(ByteOrder.LITTLE_ENDIAN)Reads and writes in a chosen byte order.
new String(bytes, "UTF-16LE")Decodes little-endian UTF-16 text directly.
Character.charCount(cp)How many chars, not bytes, a code point needs.

Java Examples

Example 1 — Swap the Bytes of Four Values

Swap a test pattern, a letter, a single high byte, and a value whose bytes are equal.

Java
public class CharacterReverseBytesValues {
    public static void main(String[] args) {
        char[] values = {0x1234, 'A', 0xFF00, 0x7777};
        for (char v : values) {
            char r = Character.reverseBytes(v);
            System.out.println("0x" + Integer.toHexString(v) + " -> 0x" + Integer.toHexString(r) + " (" + (int) r + ")");
        }
    }
}

How It Works

  1. The test pattern. 0x12 and 0x34 trade places, giving 0x3412, which is 13330 in decimal.
  2. A letter becomes a big number. 'A' is 0x0041; after the swap it is 0x4100 (16640), no longer a letter.
  3. Symmetric values. 0x7777 has two identical bytes, so swapping changes nothing; 0xFF00 becomes 0x00FF.

Example 2 — Decode Little-Endian Bytes That Were Read the Wrong Way

"Hi" stored as UTF-16LE is 48 00 69 00. Reading pairs as big-endian gives the wrong chars; reverseBytes fixes them.

Java
public class CharacterReverseBytesLittleEndian {
    public static void main(String[] args) {
        byte[] data = {0x48, 0x00, 0x69, 0x00};
        StringBuilder raw = new StringBuilder();
        StringBuilder fixed = new StringBuilder();
        for (int i = 0; i < data.length; i += 2) {
            char c = (char) (((data[i] & 0xFF) << 8) | (data[i + 1] & 0xFF));
            raw.append(" 0x").append(Integer.toHexString(c));
            fixed.append(Character.reverseBytes(c));
        }
        System.out.println("read big-endian:" + raw);
        System.out.println("after reverseBytes: " + fixed);
    }
}

How It Works

  1. Little-endian data. Each char is stored low byte first, so 'H' (0x0048) appears as the bytes 48 00.
  2. The wrong assumption. Combining each pair as high byte first gives 0x4800 and 0x6900, which are not the intended letters.
  3. One swap per char. reverseBytes turns 0x4800 into 0x0048 and 0x6900 into 0x0069, recovering Hi.

Example 3 — reverseBytes vs Writing and Reading in Different Byte Orders

Write 'A' little-endian, read it back big-endian, and compare with reverseBytes. Also check that two swaps undo each other for every char.

Java
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class CharacterReverseBytesBuffer {
    public static void main(String[] args) {
        ByteBuffer buf = ByteBuffer.allocate(2);
        buf.order(ByteOrder.LITTLE_ENDIAN).putChar(0, 'A');
        char misread = buf.order(ByteOrder.BIG_ENDIAN).getChar(0);
        System.out.println("misread: 0x" + Integer.toHexString(misread));
        System.out.println("same as reverseBytes: " + (misread == Character.reverseBytes('A')));
        int broken = 0;
        for (int i = 0; i <= 0xFFFF; i++) {
            char c = (char) i;
            if (Character.reverseBytes(Character.reverseBytes(c)) != c) broken++;
        }
        System.out.println("double swap mismatches: " + broken);
    }
}

How It Works

  1. Write one way, read the other. The buffer stores 'A' as 41 00; reading big-endian treats 0x41 as the high byte and gets 0x4100.
  2. Exactly the swap. That misread value equals Character.reverseBytes('A').
  3. Reversible. Swapping twice returns the original for all 65536 chars, so no information is ever lost.

Frequently Asked Questions

It swaps the two bytes of a char: 0x1234 becomes 0x3412.
When 16-bit data was written in one byte order (for example little-endian UTF-16) and read in the other.
Yes, always. Swapping twice restores the original value.
Not necessarily. It is just a 16-bit value; it may be unassigned or even a surrogate.
Usually yes: decode with the correct charset, such as UTF-16LE, or use ByteBuffer with the right ByteOrder.

Next: toChars()

Convert a code point into its UTF-16 chars: one char, or a surrogate pair.

toChars() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful