Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String encoding and decoding

 
Sajin Balakrishnan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not able to print other language characters.
What could be the problem?
Sample code:



 
Tim Moores
Bartender
Posts: 2743
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you trying to print the characters? Is it somewhere that can handle umlauts and such?
 
Stephan van Hulst
Bartender
Pie
Posts: 5590
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are possibly three things going wrong. The first is that you're not telling System.out what encoding you want to use to print. It doesn't matter if you've correctly stored the characters in a String, the output system still needs to know what encoding you want to print the data in.

The second problem is that even if your program prints in the encoding you want, the program that's receiving the data (probably the command prompt, or maybe your IDE console) may not expect this encoding, and interpret the data incorrectly.

The third problem is that even if the console understands exactly what character you want to print, its font does not have a graphical glyph to represent the character, and displays it with a surrogate instead.

I recommend you write a small GUI with a text area, that uses the MS Gothic font. Now try to append different characters to it.
 
Sajin Balakrishnan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually the problem in converting xml to excel.greek letters are stored in database which when read from database and exported in excel using below method.appears as ??? in excel

 
Stephan van Hulst
Bartender
Pie
Posts: 5590
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sajin, please UseCodeTags when posting code. I have added them to your post, but remember to do it the next time.

Why are you using getBytes() all over the place?
 
Sajin Balakrishnan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok..beacuse i am writing it to a FileOutputSteam hence convertin it to bytes
 
Stephan van Hulst
Bartender
Pie
Posts: 5590
55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is part of why your program doesn't work as intended. You should not mess around with the raw bytes. Instead, there are some great classes in the standard library that will make life easier for you.

Try using the following:
Now you will be able to simply write the String you want to the Writer, and it will encode it to the correct character set.
 
Sajin Balakrishnan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Stephan , it worked.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic