首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将Unicode +数字打印为Unicode

将Unicode +数字打印为Unicode
EN

Stack Overflow用户
提问于 2015-06-21 05:29:16
回答 1查看 46关注 0票数 0
代码语言:javascript
复制
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 a a 98 \u0062

我试图将这种情况下的代码1添加到a中并打印b,但我只能将它添加到unicode或ascii中,但我不能从那里返回到b。

EN

回答 1

Stack Overflow用户

发布于 2015-06-21 09:27:38

char类型的值(这里是'a' )和int类型的值(这里是1)进行算术加法运算,得到类型为int的值,其中包含'b'的(Unicode)字符代码,但不是char类型。要获取char类型的值,请使用强制转换(char)(msg[i]+code)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30959089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档