Java Character reverseBytes()及示例
java.lang.Character.reverseBytes() 是java中的一个内置方法,用于返回通过颠倒指定char值中的字节顺序而得到的值。
语法
public static char reverseBytes(char ch)
参数: 该函数接受一个强制性参数ch,它表示要颠倒字节顺序的字符。
返回: 该方法返回通过颠倒(或等同于交换)指定字符值中的字节而得到的值。
以下程序说明了java.lang.Character.reverseBytes()函数:
程序1 :
// 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);
}
}
输出:
Reversing bytes on ? gives M
程序2
// 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);
}
}
输出:
Reversing bytes on 9 gives ?
Reversing bytes on 7 gives ?