2011-12-23 6 views
2

Javaコードで「Календари」を取得する代わりに、文字列「ÐалÐμнÐ'аÐÐÐ 'を取得しています。どのように私は 'Календари'に 'ÐалÐμнÐ'аÐ'Ð'を変換できますか?JavaコードでUTF-8の問題

私はあなたのコードは大丈夫であると考えてい

String convert =new String(convert.getBytes("iso-8859-1"), "UTF-8") 
String convert =new String(convert.getBytes(), "UTF-8") 
+3

あなたは、コードサンプルを少し広げることができますか?入力がどこから来ているのか明確ではありません。 ioを読んでいるのか、メモリに文字列があるのか​​はっきりしないのですか? – Bill

+0

私のコードで私の最初の行が動作します –

+0

どこから始まる文字列はどこですか?あなたが表示した文字をエンコードできないISO-8859-1を使用しているのはなぜですか? – deceze

答えて

4

を使用。あなたの問題は特定の文字変換を行う必要があり、実際の入力が正しくエンコードされていない可能性があると思われます。テストするために、私は、CharSetエンコーディング/デコードのステップごとに標準的な手順を実行して、どこが壊れているかを確認します。

あなたのエンコーディングは、罰金に見える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); 
+2

はい私はこのコードを試してみました.itsはeclipse.Athで働いていましたが、このコードをnetbeansで実行すると、このような結果の文字列が '???????' – chinchu

+0

これは、JDKの問題または入力の問題....? charset.isEncodngSupportedで確認してください – jayunit100

2

はUnicode値の代わりに文字列リテラルを使用してください。詳細については、以下を参照してください。list of Unicode charactersについて

  1. Russian on-screen keyboard(Unicode値のために上にマウスを移動)
  2. そして、どのように?

編集 - それは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