将Unicode +数字打印为Unicode

时间:2015-06-20 21:29:16

标签: java unicode ascii

public static void main (String[] args) {

    char[] msg;
    int code;
    int i;
    String newMsg;

    msg = getMsg(); // Read the message from keyboard
    code = getCode();

    System.out.println("Code : "+code);
    for (i=0; i<msg.length; i++){
        System.out.println(msg[i]);
        System.out.println(Character.toString((char)msg[i]));
        newMsg = ( "\\u" + Integer.toHexString(msg[i] + code  | 0x10000).substring(1));
        System.out.println (String.valueOf(msg[i] + code ));
        System.out.println (newMsg);
}

public static int getCode(){
    int code=0;
    System.out.print("Input Code: ");
    Scanner input = new Scanner(System.in);
    return input.nextInt();
}

public static char[] getMsg(){
    String myMessage;
    System.out.print("Input Message: ");
    Scanner input = new Scanner(System.in);
    myMessage = input.nextLine();// Read a line of message
    return myMessage.toCharArray();
}

我的输出如下:

输入消息:a 输入代码:1 代码:1 一个 一个 98 \ u0062

我试图在这种情况下将CODE添加到a并打印b,但我只能将它添加到unicode或ascii,但我不能从那里回到b。

1 个答案:

答案 0 :(得分:0)

此处+类型char的值的算术加法'a'和此处的类型int的值1会生成int类型的值,此处为包含'b'的(Unicode)字符代码,但不是类型 char。要获取char类型的值,请使用强制转换(char)(msg[i]+code)