Home Java Character Methods reverseBytes() Java Character reverseBytes() Method 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.
char a = Character.reverseBytes((char) 0x1234); // 0x3412
char b = Character.reverseBytes('A'); // 0x4100 (16640)
char c = Character.reverseBytes(b); // 'A' again Call What 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.lang.Character (since Java 5)
public static char reverseBytes(char ch) Parameters Parameter Description ch (char)The value whose bytes are swapped.
Return Value Return Value Description charThe value with its high and low bytes exchanged.
Important
This is a bit operation, not a text operation: the result is usually not a meaningful character, and it can even be a lone surrogate. Use it on raw data only. For whole buffers, ByteBuffer.order(...) or a charset such as UTF-16LE is usually simpler. It never throws.
Compare with Similar Methods Related tools:
Method How 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 1. Swap some values 2. Fix a byte-order mistake 3. Compare with ByteBuffer 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.
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 + ")");
}
}
} 0x1234 -> 0x3412 (13330)
0x41 -> 0x4100 (16640)
0xff00 -> 0xff (255)
0x7777 -> 0x7777 (30583) How It Works The test pattern. 0x12 and 0x34 trade places, giving 0x3412, which is 13330 in decimal.A letter becomes a big number. 'A' is 0x0041; after the swap it is 0x4100 (16640), no longer a letter.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.
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);
}
} read big-endian: 0x4800 0x6900
after reverseBytes: Hi How It Works Little-endian data. Each char is stored low byte first, so 'H' (0x0048) appears as the bytes 48 00.The wrong assumption. Combining each pair as high byte first gives 0x4800 and 0x6900, which are not the intended letters.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.
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);
}
} misread: 0x4100
same as reverseBytes: true
double swap mismatches: 0 How It Works 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.Exactly the swap. That misread value equals Character.reverseBytes('A').Reversible. Swapping twice returns the original for all 65536 chars, so no information is ever lost.Frequently Asked Questions What does Character.reverseBytes do? It swaps the two bytes of a char: 0x1234 becomes 0x3412.
When would I need reverseBytes? When 16-bit data was written in one byte order (for example little-endian UTF-16) and read in the other.
Does reverseBytes(reverseBytes(c)) return c? Yes, always. Swapping twice restores the original value.
Is the result a valid character? Not necessarily. It is just a 16-bit value; it may be unassigned or even a surrogate.
Is there an easier way for whole files? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion