Java Character.reverseBytes() Method

Last Updated : 14 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The Character.reverseBytes() method in Java is a utility function from the java.lang.Character class. This method is used to reverse the order of the two bytes in a given 16-bit Unicode character (char). All Java characters are two bytes. It is useful when we work with systems or protocols that use a different byte order.

Syntax of Character.reverseBytes() Method

public static char reverseBytes(char ch)

  • Parameter: The function accepts one mandatory parameter, "ch" which signifies the character whose order of bytes is to be reversed. 
  • Returns: This method returns the value obtained by reversing (or, equivalently, swapping) the bytes in the specified char value.

When to Use Character.reverseBytes() Method

  • When we work with systems that use different byte orders.
  • When reading binary files or network data and byte alignment is important.
  • For low-level optimizations in memory or protocol parsing.

Examples of Java Character.reverseBytes() Method

Example 1: In this example, we are going to reverse the Unicode character bytes.

Java
// Java program to demonstrate 
// Character.reverseBytes() on a Unicode character
import java.lang.Character;

public class Geeks {
    
    public static void main(String[] args) {
        
        // character with Unicode value \u4d00 (two bytes)
        char ch1 = '\u4d00';

        // reversing the byte order of the character
        char ch2 = Character.reverseBytes(ch1);

        System.out.println("Original char: " + ch1);
        System.out.println("After reversing bytes: " + ch2);
    }
}

Output
Original char: ä´€
After reversing bytes: M


Example 2: In this example, we are going to reverse the bytes of digit characters.

Java
// Java program to demonstrate 
// Character.reverseBytes() on numeric characters
import java.lang.Character;

public class Geeks {
    
    public static void main(String[] args) {
        
        // reversing byte order of character '9'
        char ch1 = '9';
        char reversed1 = Character.reverseBytes(ch1);
        System.out.println("Reversing bytes on '" + ch1 + "' gives: '" 
        + reversed1 + "'");

        // reversing byte order of character '7'
        char ch2 = '7';
        char reversed2 = Character.reverseBytes(ch2);
        System.out.println("Reversing bytes on '" + ch2 + "' gives: '" 
        + reversed2 + "'");
    }
}

Output
Reversing bytes on '9' gives: '㤀'
Reversing bytes on '7' gives: '㜀'

Note: The reversed values may appear as special Unicode symbols and depends on their byte combinations.

Important Points:

  • This method is very helpful for handling byte-level transformations of characters.
  • It swaps the two bytes in a 16-bit char.
  • The results may not always be a human readable character, because it depends on the Unicode value formed by the reversed bytes.

Next Article
Practice Tags :

Similar Reads