J2ME and Unicode - Reading a Text File with Unicode Codes
(Page 5 of 6 )
In the last section we went over reading a Unicode file. An alternative approach is to read the text file with Unicode codes in them. Referring back to the Hello World example the following would be inside the text file:
u3053u3093u306Bu3061u306Fu4E16u754C
One would think this is very straight forward and that a simple load/read of the text would do. However, when reading the file each character is treated as a string. So in other words:
u3053
reads in as
‘’, ‘u’, ‘3’,’0’,’5’,’3’
You will now have to detect and parse out the u which indicates that the next four characters represent a Unicode character. The following method will help convert the string to the appropriate Unicode character. It assumes you are only passing in a valid four character Unicode (ie: 3053).
private String convertStrToUnicode(String value) {
short valueAsShort = Short.parseShort(value.trim(),16);
return String.valueOf((char)valueAsShort);
}
Next: Conclusion >>
More Java Articles
More By Jason Lam