Programming

將字元轉換成ASCII值

將字元轉換成ASCII值

以下是幾種方法可以將字元轉換為 ASCII 值:

1. 使用 (int) 類型轉換:


char c = 'a';
int asciiValue = (int) c;
System.out.println(asciiValue);  // 輸出: 97

2. 使用 Character.getNumericValue 方法:


char c = 'A';
int asciiValue = Character.getNumericValue(c);
System.out.println(asciiValue);  // 輸出: 10

3. 使用數學運算,減去對應的 ASCII 值:


Copy code
char c = '0';
int asciiValue = c - '0';
System.out.println(asciiValue);  // 輸出: 0