2

I'm getting a string 'ÐалендаÑÐ' instead of getting 'Календари' in Java code. How can I convert 'ÐалендаÑÐ' to 'Календари'?

I used

 String convert =new String(convert.getBytes("iso-8859-1"), "UTF-8") 
 String convert =new String(convert.getBytes(), "UTF-8") 
Lion
  • 18,729
  • 22
  • 80
  • 110
divz
  • 7,847
  • 23
  • 55
  • 78

2 Answers2

4

I believe your code is okay. It appears that your problem is that you need to do a specific character conversion, and maybe your "real" input is not being encoded correctly. To test, I would do a standard step by step CharSet encoding/decoding, to see where things are breaking.

Your encodings look fine, http://docs.oracle.com/javase/1.6/docs/guide/intl/encoding.doc.html

And the following seems to run normally :

//i suspect your problem is here - make sure your encoding the string correctly from the byte/char stream. That is, make sure that you want "iso-8859-1" as your input characters. 

Charset charsetE = Charset.forName("iso-8859-1");
CharsetEncoder encoder = charsetE.newEncoder();

//i believe from here to the end will probably stay the same, as per your posted example.
Charset charsetD = Charset.forName("UTF-8");
CharsetDecoder decoder = charsetD.newDecoder();

ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(inputString));
CharBuffer cbuf = decoder.decode(bbuf);
final String result = cbuf.toString();
System.out.println(result);
jayunit100
  • 17,388
  • 22
  • 92
  • 167
  • 2
    Yes i tried this code also..its working in eclipse.Ath the same time when i run this code in netbeans the resulting string look like this'???????' – divz Dec 23 '11 at 06:13
  • That could be a jdk issue or a input issue....? Check with charset.isEncodngSupported – jayunit100 Dec 23 '11 at 12:48
2

Use the Unicode values instead of string literals. For more information, see:

  1. Russian on-screen keyboard (hover over for Unicode values)
  2. And how about a list of Unicode characters?

Edit -
Note that it's important to use an output font that supports displaying Unicode values (e.g. Arial Unicode MS).

Example -

import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

final class RussianDisplayDemo extends JFrame 
{
    private static final long serialVersionUID = -3843706833781023204L;

    /**
     * Constructs a frame the is initially invisible to display Russian text
     */
    RussianDisplayDemo()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(new FlowLayout());
        add(getRussianButton());
        setLocationRelativeTo(null);
        pack();
    }

    /**
     * Returns a button with Russian text
     * 
     * @return a button with Russian text
     */
    private final JButton getRussianButton()
    {
        final JButton button = new JButton("\u042da\u043d\u044f\u0442\u043e"); // Russian for "Busy"
        return button;
    }

    public static final void main(final String[] args) 
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public final void run() 
            {
                final RussianDisplayDemo demo = new RussianDisplayDemo();
                demo.setVisible(true);
            }
        });
    }
}

enter image description here

mre
  • 43,520
  • 33
  • 120
  • 170