Java中的字符.reverseBytes() 和示例
Java.lang。 字符.reverseBytes()是Java中的内置方法,它返回通过反转指定char值中的字节顺序获得的值。
句法:
public static char reverseBytes(char ch)
参数:该函数接受一个强制参数ch ,它表示要颠倒字节顺序的字符。
返回:此方法返回通过反转(或等效地交换)指定 char 值中的字节获得的值。
下面的程序说明了Java.lang。字符.reverseBytes()函数:
方案一:
// 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
方案二:
// 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 ?