UTF-8 issue in Java code

I get the string “REL Д енÐ'Ð ° ÐÐ ′ instead of getting the “Calendars” in the Java code. How can I convert 'Ð ° Ð? ÐμнÐ'Ð ° ÑÐ 'in' Calendars'?

I used

 String convert =new String(convert.getBytes("iso-8859-1"), "UTF-8") 
 String convert =new String(convert.getBytes(), "UTF-8") 
+2
source share
2 answers

I believe your code is ok. It seems that your problem is that you need to perform a specific character conversion, and maybe your "real" input is incorrectly encoded. To test, I would do a standard CharSet step-by-step encoding / decoding to see where something breaks.

, http://docs.oracle.com/javase/1.6/docs/guide/intl/encoding.doc.html

, , :

//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);
+4

Unicode . .:

  • ( Unicode)
  • ?

-
, , Unicode (, Arial Unicode MS).

-

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

+2

All Articles