简体   繁体   中英

JAVA String to char

I have a String representing the hex value of a char, such as: "0x6d4b". How can I get the character it represents as a char?

String c = "0x6d4b";
char m = ???
// Drop "0x" in order to parse
String c = "6d4b";
// Parse hexadecimal integer
int i = Integer.parseInt( c, 16 );
// Note that this method returns char[]
char[] cs = Character.toChars( i );
// Prints 测
System.out.println( cs );
String s = "6d4b";
int i = Integer.parseInt( s, 16 );   // to convert hex to integer
char ca= (char) i;
System.out.println(ca);
System.out.println((char)Integer.parseInt("6d4b",16));

Try this,

String s ="0x6d4b" ;
        char[] c = s.toCharArray();

        for (char cc : c){

            System.out.print(cc);
        }

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