Trying to display Unicode characters
Hello,
I'm not sure where to post this. I'm trying to write a simple GUI Unicode Viewer. The user enters the Hex code in the JTextField and the program generates the Unicode character for that code as well as the next 320 Unicode characters ad displays them in a JTextArea. However, when I try to insert the "\u" escape character in the front of the Hex code I get an "illegal unicode escape" compiler error. How do I fix this? I tried converting the hex string to an int and explicitly cast it to a char which worked for small values but caused a NumberFormatException on larger values
Here is the code:
Code :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class Exercise31_1 extends JApplet {
private JTextField jtfUnicode = new JTextField(5);
private JTextArea jtaUnicode = new JTextArea();
private String hexCode;
private int hexNumber;
public void init() {
JPanel p1 = new JPanel(new BorderLayout());
p1.add(jtfUnicode, BorderLayout.CENTER);
p1.setBorder(new TitledBorder("Specify Unicode"));
JPanel p2 = new JPanel(new BorderLayout());
p2.add(new JScrollPane(jtaUnicode), BorderLayout.CENTER);
jtaUnicode.setEditable(false);
add(p1, BorderLayout.NORTH);
add(p2, BorderLayout.CENTER);
//jtaUnicode.append("\u4F20");
jtfUnicode.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
hexCode = jtfUnicode.getText().trim();
hexNumber = HexToDecimalConversion.hexToDecimal(hexCode);
for (int i = 0; i < 20; i++) {
jtaUnicode.append(hexCode + " ");
for (int j = 1; j < 16; j++) {
hexNumber++;
hexCode = Decimal2HexConversion.decimalToHex(hexNumber);
jtaUnicode.append(hexCode+ ",");
}
jtaUnicode.append("\n\n");
hexNumber++;
hexCode = Decimal2HexConversion.decimalToHex(hexNumber);
}
}
});
}
public static void main(String[] args) {
Exercise31_1 applet = new Exercise31_1();
JFrame frame = new JFrame("Unicode viewer");
frame.add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(500, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Thanks in advance for the help.
Casey
Re: Trying to display Unicode characters
The error I get is
Code :
cannot find symbol - HexToDecimalConversion
on this line:
Code Java:
hexNumber = HexToDecimalConversion.hexToDecimal(hexCode);
Is this the error you are trying to solve or is it just missing?
Re: Trying to display Unicode characters
Quote:
Originally Posted by
ChristopherLowe
The error I get is
Code :
cannot find symbol - HexToDecimalConversion
on this line:
Code Java:
hexNumber = HexToDecimalConversion.hexToDecimal(hexCode);
Is this the error you are trying to solve or is it just missing?
Sorry, no. HexToDecimalConversion is another program I wrote and that works fine. I just realized that I took out the bad code. This is the line that was causing me trouble
jtaUnicode.append("\u" + hexCode);
The compiler error I get is "illegal unicode escape "
The code I listed runs but, instead of the Unicode character being displayed, it displays the hex number. And when I try to insert the \u escape in front of the hex number I get the compiler error.
Re: Trying to display Unicode characters
\u is a compiler feature allowing you to enter Unicode in your source program
In your source there is no value after the \u so the compiler complains.
What data type is hexCode? What does it contain? How do you want it converted to a String to display in the text area? Can you give an example?
Re: Trying to display Unicode characters
Quote:
Originally Posted by
Norm
\u is a compiler feature allowing you to enter Unicode in your source program
In your source there is no value after the \u so the compiler complains.
What data type is hexCode? What does it contain? How do you want it converted to a String to display in the text area? Can you give an example?
hexCode is a String variable. The user enters a four digit hex code (ie 4F20) and the text area is supposed to display the Unicode character for that hexCode and the next 320 characters. But all it displays of course is the hexcode. But if I hard code "\u4F20" into the append method it displays the appropriate character (in this case a chinese character). I tried to combine the string hexcode with the string \u to get the desired result but it is obviously illegal to do that. The only reason I am am converting from hex to decimal and back is so that I can increment the hex number to display the subsequent characters.
What i want to do is find a way to take the hex code the user enters and display the eqvialent Unicode character.
Re: Trying to display Unicode characters
Given a String as input, you could use an Integer parse method to convert that to an int value which you could then increment easily and convert its new value to hex again by using another of the Integer class's methods.
Quote:
take the hex code the user enters and display the eqvialent Unicode character.
Have you tried casting to char?
Re: Trying to display Unicode characters
I tried that and it worked with smaller values like 0077 but I got a NumberFormatException on larger values like 4F20.
Re: Trying to display Unicode characters
Quote:
I got a NumberFormatException on larger values like 4F20.
Can you post code that gets that error?
Re: Trying to display Unicode characters
Quote:
Originally Posted by
Norm
Can you post code that gets that error?
Unfortunately, I'm not at home right now, so I'll have to post it on Monday. But, if memory serves me correctly, I used int hexNumber = Integer.parseInt(hexCode) to convert the hexCode string to an int and then used char hexChar = (char)hexNumber to cast it.
Re: Trying to display Unicode characters
Check the parseInt method's API doc. It defaults to base 10. 4F20 is not base 10 so you will have to specify what base you what the parseInt method to use.
Re: Trying to display Unicode characters
Quote:
Originally Posted by
Norm
Check the parseInt method's API doc. It defaults to base 10. 4F20 is not base 10 so you will have to specify what base you what the parseInt method to use.
That could definetely be the problem with the NumberFormatException error. I think I will try taking the string from the user input and converting it to decimal first with my conversion program (which returns an int) and cast the result to char. I will let you know if it works.
Thanks.
Re: Trying to display Unicode characters
Or, just call the API function to do it for you.
Re: Trying to display Unicode characters
Thanks to Norm and everyone else. Using Integer.parseInt(String s, int radix) worked. It allowed me to cast the int hexCode to a char :)