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") 
4
  • 3
    Can you expand the code sample a little bit? Its not clear where the input is coming from. Also not clear if you're reading io, or have a string in memory? Commented Dec 23, 2011 at 5:43
  • the first line works for me in my code Commented Dec 23, 2011 at 5:47
  • Where is the string coming from to begin with? Why are you using ISO-8859-1, which cannot encode the characters you have shown? Commented Dec 23, 2011 at 5:59
  • What are you using to display the string? Commented Dec 23, 2011 at 6:43

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

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'???????'
That could be a jdk issue or a input issue....? Check with charset.isEncodngSupported
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.