6

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 = ???
1

4 Answers 4

9
// 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 );
Sign up to request clarification or add additional context in comments.

2 Comments

are you sure it's not just (char)i?
Yes, if the resulting value is used as a char, the results are the same. The method call gives you the added bonus of readily available javadoc that tells what you're actually getting.
2
String s = "6d4b";
int i = Integer.parseInt( s, 16 );   // to convert hex to integer
char ca= (char) i;
System.out.println(ca);

1 Comment

all in one line version: System.out.println ( (char)Integer.parseInt(c.substring(2), 16) );
0
System.out.println((char)Integer.parseInt("6d4b",16));

Comments

-1

Try this,

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

        for (char cc : c){

            System.out.print(cc);
        }

2 Comments

have you tested it before posting because its not working. and by the way its not what is asked. So -1
@MohammadAdil It works on my side (compilation wise) but you're right, it's not what's wanted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.