Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Printing Unicode characters  RSS feed

 
raghu dubey
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using the below code to print all the 65000 characters in unicode, like @,(TM)etc. But I only get a '?' printed 65000 times. Why is that so.

=======
package chap_3;

class PrimitiveType {

public static void main(String[] args) {
// TODO Auto-generated method stub
char a=0;

while(a<=65000){


String hexVal=Integer.toHexString(a);
//int c1=Integer.parseInt(hexVal,16);
String c1="\\u"+hexVal;
char c2=(char)(Integer.parseInt(hexVal,16));
System.out.println(Integer.toHexString(a));
System.out.println("\u2122 \u005D");//this line works
System.out.println(c1);//but this one just prints the unicode values itself and not the actual characters.
System.out.println(c2);//thos one prints ? all the time.
a++;
}

}

Could anyone please tell me how to accomplish this.

thanks
Raghu.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
c1 doesn't work because it consists of a backslash, a "u" character, and then a few more characters. You can't construct Unicode characters this way.

Why not simply do "System.out.println(new Character((char) a))"?
[ September 02, 2007: Message edited by: Ulf Dittmer ]
 
raghu dubey
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for response,

I already tried that, but it still prints just '?'. Dont know why does that happen.
 
raghu dubey
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println("\u2122 \u005D");

this line works correctly and I see the trademark symbol and "]". But how to do that for all the unicode characters in a loop.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!