Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File reading issue (Unicode)

Status
Not open for further replies.

subin83

Programmer
Aug 7, 2010
5
DE
Hi All,

I am facing an issue which I couldnt solve after much repeated search. Hope you all can help me.
I have a txt file(ASCII) containing the decimal representation of an Unicode ex:(កម្មវិធី)
I have a function which will convert this decimal value to hexadecimal unicode ie in this case (\u1780\u1798\u17d2\u1798\u179c\u17b7\u1792\u17b8)

The logic is that I take each line from the file, convert it into unicode hex representation and then write to a JPG file.
The issue here is with the hexValue variable. Its getting the correct value but when I use it in the function call i.e. ConvertUnicode(image, hexValue); its not working. If I equate the variable with the corresponding value(ie \u1780..) its working fine. I checked for extra carriage returns, spaces etc but still issue is there.

I tried printing decoded = "-"+decoded+"-"; in the function and got -\u1780\u1798\u17d2\u1798\u179c\u17b7\u1792\u17b8- and showing no extra characters.

Am I doing some mistake in the file encoding part? I tried with UTF encoding and read appropriately but still same issue is there. I would be very thankful for any pointers regarding this.


String strLine = "";
String hexValue;
try {

FileInputStream fstream = new FileInputStream(args[0]);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in);

while ((strLine = br.readLine()) != null) {

hexValue = ConvertDectoHex(strLine); // though hexValue gives \u1780\u1798\u17d2\u1798\u179c\u17b7\u1792\u17b8 as output, it not able to display the font on the image

//hexValue = "\u1780\u1798\u17d2\u1798\u179c\u17b7\u1792\u17b8"; if I set it like this I am able to get everything fine

File file = new File("blank.jpg");
BufferedImage image = ImageIO.read(file);
System.out.println(hexValue);
new ConvertUnicode(image, hexValue);
}
in.close();



--------------------------

public static String ConvertDectoHex(String dec) {
String temp;
String[] ret_coma;
temp = dec.replaceAll("&#", "");

ret_coma = semicommaSeparatedStringToStringArray(temp);

for (i=0; i< ret_coma.length ; i++) {

ret_coma = ret_coma.trim();
if (ret_coma.length() > 0 && ret_coma != null) {
hexstr = Integer.toHexString(Integer.parseInt(ret_coma));
decoded = decoded + "\\u" + hexstr;
}
}
return decoded;
}

private static String[] semicommaSeparatedStringToStringArray(String aString){
String[] splittArray = null;
if (aString != null || !aString.equalsIgnoreCase("")){
splittArray = aString.split(";");
//System.out.println(aString + " " + splittArray);
}
return splittArray;
}

public ConvertUnicode(BufferedImage image, String ttxt) throws Exception {
drawText(image, ttxt);
File file = save(image);
show(file);
}

Regards,
Subin



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top