2013年5月8日 星期三

ASCII 轉全形字

   
public class halfToWhole {
    public static void main(String[] args) {
        System.out.println(halfShapeTransWholeShape('~'));
    }
}

public static String halfShapeTransWholeShape(String waitTransString){
    String concatenation = ""; //儲存回傳的String
    for(int i=0; i < waitTransString.length(); i++){
        char character = waitTransString.charAt(i);
        concatenation += halfShapeTransWholeShape(character);
    }
    return concatenation;
}

//鍵盤上能按的符號有94個符號(扣掉32的空格)轉成全形
public static char halfShapeTransWholeShape(char waitTransChar){
    int checkChar = waitTransChar; //轉成ASCII的數字
    if(checkChar > 32 && checkChar < 127){
        return (char) (checkChar+0xFEE0);
    } else
    //如果是半形空格,直接給死的全形空格
    if(checkChar == 32) { // 全形空格的 Unicode 是12288,16 進制是 3000
        return " ";
    }
    return waitTransChar;
}

※FEE0的由來:

int questionHalf = '!';// 半形驚嘆號
int questionWhole = '!';// 全形驚嘆號
int exclamationHalf = '?';// 半形問號
int exclamationWhole = '?';// 全形問號

// 全形減半形都是65248
Integer question = questionWhole - questionHalf;
Integer exclamation = exclamationWhole - exclamationHalf;
System.out.println(question);
System.out.println(exclamation);
// 16進位是FEE0
System.out.println(Integer.toHexString(question));
System.out.println(Integer.toHexString(exclamation));


沒有留言:

張貼留言