Character.reverseBytes() in Java with examples

java.lang.Character.reverseBytes() is a built-in method in java which returns the value obtained by reversing the order of the bytes in the specified char value.

Syntax:

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.

Below programs illustrate the java.lang.Character.reverseBytes() function:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the
// Character.reverseBytes() when a
  
import java.lang.*;
  
public class gfg {
  
    public static void main(String[] args)
    {
  
        // char whose byte order is to be reversed
        char c1 = '\u4d00';
        char c2 = Character.reverseBytes(c1);
  
        // prints the reversed bytes
        System.out.println("Reversing bytes on " + c1 + " gives " + c2);
    }
}

chevron_right


Output:

Reversing bytes on ? gives M

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the
// Character.reverseBytes() when a
// number is passed
  
import java.lang.*;
  
public class gfg {
  
    public static void main(String[] args)
    {
  
        // char whose byte order is to be reversed
        char c1 = '9';
        char c2 = Character.reverseBytes(c1);
  
        // prints the reversed bytes
        System.out.println("Reversing bytes on " + c1 + " gives " + c2);
  
        c1 = '7';
        c2 = Character.reverseBytes(c1);
  
        // prints the reversed bytes
        System.out.println("Reversing bytes on " + c1 + " gives " + c2);
    }
}

chevron_right


Output:

Reversing bytes on 9 gives ?
Reversing bytes on 7 gives ?


My Personal Notes arrow_drop_up

Contented with little yet wishing for more

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.