简体   繁体   中英

What is the value of a Char in java?

Let's say I have a 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 . Then I tried 𐅑 and got d800 dd51 which seems not to be a UTF value.

Just wondering, what is the int value of a Char in Java.

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? 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:

0061
0062
0063
0064
0065
0066

Which is exactly what your print statement instructs:

-cast the character to its' ascii value

-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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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