It's not a secret anymore!*
The moose likes Java in General and the fly likes String encoding and decoding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "String encoding and decoding" Watch "String encoding and decoding" New topic
Author

String encoding and decoding

Sajin Balakrishnan
Greenhorn

Joined: Feb 21, 2012
Posts: 11
I am not able to print other language characters.
What could be the problem?
Sample code:



Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Where are you trying to print the characters? Is it somewhere that can handle umlauts and such?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3599
    
  14

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

Joined: Feb 21, 2012
Posts: 11
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

Joined: Sep 20, 2010
Posts: 3599
    
  14

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

Joined: Feb 21, 2012
Posts: 11
ok..beacuse i am writing it to a FileOutputSteam hence convertin it to bytes
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3599
    
  14

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

Joined: Feb 21, 2012
Posts: 11
Thanks a lot Stephan , it worked.
 
 
subject: String encoding and decoding
 
Similar Threads
encoding URL in (href element of) JSP without scriptlets
Trying to POST an XML to php based Web Service via HTTP POST method
Getting the MAC address and using that for licensing
Setting strings explicitly and using JOptionPane - UTF Character conversion problems...
post url connection not working