當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。