当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Character reverseBytes()用法及代码示例


Character 类的 reverseBytes(char ch) 方法返回通过按特定顺序反转给定值的字节而获得的值。

用法

public static char reverseBytes(char ch)

参数

ch: 这是需要测试的字符。

返回值

reverseBytes(char ch) 方法返回通过反转指定字符值中的字节而获得的值。

例子1

import java.util.Scanner;
public class JavaCharacterReverseBytesExample1 {
	public static void main(String[] args) {
	// Ask the user to enter the input. 
	   System.out.print("Enter the first input:");
	// Use Scanner classs to get the user input.
	   Scanner s = new Scanner(System.in);
        // Gets the user input.
	   char[] value1 = s.nextLine().toCharArray();
		
	 // Get the result by reversing the bytes of character input.
		for (char ch:value1) {
		char result1 = Character.reverseBytes(ch);
	// Print the result.
System.out.println("The reversing of the character '" + ch + "' will provide the result as:" + (int)result1);
          }  
         System.out.print("Enter the second input:");
         char[] value2 = s.nextLine().toCharArray();
         for (char ch2:value2) {
	char result2= Character.reverseBytes(ch2);
        System.out.println("The reversing of the character '" + ch2 + "' will provide the result as:" + (int)result2);
    }
}
}

输出:

Enter the first input:Helllo
The reversing of the character 'H' will provide the result as:18432
The reversing of the character 'e' will provide the result as:25856
The reversing of the character 'l' will provide the result as:27648
The reversing of the character 'l' will provide the result as:27648
The reversing of the character 'l' will provide the result as:27648
The reversing of the character 'o' will provide the result as:28416
Enter the second input:World
The reversing of the character 'W' will provide the result as:22272
The reversing of the character 'o' will provide the result as:28416
The reversing of the character 'r' will provide the result as:29184
The reversing of the character 'l' will provide the result as:27648
The reversing of the character 'd' will provide the result as:25600

例子2

public class JavaCharacterReverseBytesExample2 {
    public static void main(String[] args) {
      // Create two char primitives ch1 and ch2.
      char ch1, ch2;
      // Assign the values to ch1.
      ch1 = '\u5d00';
      ch2 = '\u4d00';
      // Assign the result of reverseBytes in ch3 and ch4.
       char ch3 = Character.reverseBytes(ch1);
       char ch4 = Character.reverseBytes(ch2);
      String str1 = "The reversing of bytes for the first character is given as:" + ch3;
      String str2 = "The reversing of bytes for the second character is given as:" + ch4;
      // print the value.
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

输出:

The reversing of bytes for the first character is given as:]
The reversing of bytes for the second character is given as:M




相关用法


注:本文由纯净天空筛选整理自 Java Character reverseBytes() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。