Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creation of char type during runtime

 
Kirti Dhingra
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a very simple question.
How can we create char at runtime?
I arrived at this problem after I created all the hexadecimal
codes of unicode charcters during runtime. Now the problem is,
how to display their resultant char type.
e.g.
String gp[]={"\u0001","\u0002"};
Now how we display what gp[0] stands for
K Singh
 
David Harrigan
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know if I understand the question correctly, but this works for me:-

Output : david
David Harrigan
 
Kirti Dhingra
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am saying something differrent.
e.g.
char ch=64;
System.out.println(ch);
the output would be A.
What I am saying is how can we print representations of hexadecimal unicodes which are generated dynamically.
K Singh
 
Sam Wong
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This shows how you can do it. The compiler interprets \uXXXX as unicode and translates it. To actually show the unicode itself, use \\uXXXX. This tells the compiler that you want a slash instead of the escape sequence \u. Hope this helps.
<pre>
class DisplayCharacter {
char c = '\u0064';
String s[] = {"\\u0064"};
public DisplayCharacter() {
System.out.println(c);
System.out.println(s[0]);
}
public static void main (String[] args) {
DisplayCharacter dc = new DisplayCharacter();
}
}
</pre>
Output:
<pre>
d
\u0064
</pre>
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried looking it up in the API to see if there is a toHexString method in the Character class, but there isn't. You could pass the char into the static method toHexString of the Integer Wrapper class since a char can be converted to an Int.
So you could say:
System.out.println(Intger.toHexString(c));
Where c is the char you want to display. There may be a better way to display the unicode represntation, but I am not sure what it is.
Hope that helps,
Bill
 
Kirti Dhingra
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I am saying is other way round.
for e.g. if I want to display all the ascii character, I would do ...
for(int i=0;i<256;i++)
System.out.println((char)i);
now if I want to display all the unicode characters, then ...
first get unicode pattern i.e. likely in String form
now say String gd[] contains all the possible unicode
combinations
for(int i=0;i<16*16*16*16;i++)
System.out.println("\\u"+gd[i]);
or try
for(int i=0;i<16*16*16*16;i++)
System.out.println("'\\u"+gd[i]+"'");
now say if \u0064 comes up, \u0064 is printed instead
of d.
How can I get d printed?
K Singh
 
Sam Wong
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I think I know what you're asking. You want to translate between unicode and its character equivalent during runtime. I haven't found any class within Java2 that will do this.
Assuming that you have already constructed a list of unicode strings, I would suggest that you use a Hashtable to map the unicode strings (key) to its character representation (element). Now you can get the char (Most likely using Character.charValue() since Hashtable require Objects) from the unicode string.
 
Kirti Singh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got the solution
String st="some unicode string";
System.out.println((char)st.hashCode());
K Singh
 
Sam Wong
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now, I have no idea what you really want. Casting the hashcode to a char, I don't understand.
 
Kirti Singh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suppose
String st="\\u0064";
System.out.println((char)st.hashCode()); // result will be d
I think hahCode functions converts a string to internal hexadecimal coding which then can be casted.
K Singh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic