简体   繁体   English

Java中Char的值是什么?

[英]What is the value of a Char in java?

Let's say I have a String . 假设我有一个String If I do this: 如果我这样做:

    for (int index = 0; index < ch.length(); index++) {
            char c = ch.charAt(index);
            System.out.println(String.format("%04x", (int) c));
    }

What will the output be ? 输出将是什么?

I tried a and got 0061 , which seems to be the UTF-8/ASCII value of A . 我想a ,得到了0061 ,这似乎是的UTF-8 / ASCII值A Then I tried 𐅑 and got d800 dd51 which seems not to be a UTF value. 然后我尝试𐅑并得到d800 dd51 ,它似乎不是UTF值。

Just wondering, what is the int value of a Char in Java. 只是想知道,Java中Char的int值是多少。

I believe your ch variable in your for loop is a String type and you want to access each character in that then cast it to it's ascii value? 我相信您的for循环中的ch变量是String类型,您想访问其中的每个字符,然后将其转换为ascii值? Well that's what your code does. 那就是您的代码所要做的。 I ran it after making minor corrections and using String ch = "abcdef" and it gave me the out put of: 我做了一些小的更正并使用String ch = "abcdef"来运行它,它的输出结果是:

0061
0062
0063
0064
0065
0066

Which is exactly what your print statement instructs: 您的打印语句所指示的正是以下内容:

-cast the character to its' ascii value -将角色投射到其“ ascii”值

-output four character long value. -输出四个字符的长值。

If it helps, the ascii value for a, b, c, d, e and f are 61, 62, 63, 64, 65 and 66. 如果有帮助,a,b,c,d,e和f的ascii值分别为61、62、63、64、65和66。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM