Display Binary in Swing Textarea problem

Discussions

Web tier: servlets, JSP, Web frameworks: Display Binary in Swing Textarea problem

  1. Display Binary in Swing Textarea problem (1 messages)

    Hi,

    I got the problem to display a binary file in the swing textarea.

    following is the code:

    byte[] bytes={(byte)0x8D, (byte)0x9D};
    String str=new String(bytes);
    textArea.setText(str);

    Because I used the system default charset which is UTF-8. When I convert to byte array to String, the 0x8d can't be mapped into any valid character in UTF-8 charset so that it will be converted into a '?'. So the information displayed in the textarea is wrong.

    My objective is to display binary file and can copy/paste this binary to/from other windows application without information lose.

    I have checked all the standard Charset in the JDK, it seems no one is suitable for my case.

    Anyone can help me how to solve this problem?

    Thanks
    Jianwu
  2. Encode your binary using base-64 encode in the text array. In the target textarea, decode it back to binary before saving.