Java convert ISO-8859-1 to UTF-8

18,379

This worked for me:

@Pshemo had the answer I was looking for

byte[] isoBytes = line.getBytes("ISO-8859-1");
System.out.println(new String(isoBytes, "UTF-8"));
Share:
18,379
Admin
Author by

Admin

Updated on June 06, 2022

Comments

  • Admin
    Admin almost 2 years

    I have a properties file with Asian translations in it, which I believe is saved as ISO-8859-1. I'm trying to convert them to UTF-8. So è­¦å: would equal 警告:

    I've tried several methods listed on this site as well as some other sites but have had no luck.

    byte[] isoBytes = line.getBytes("ISO-8859-1");
    byte[] utf8 = new String(isoBytes, "ISO-8859-1").getBytes("UTF-8");
    

    CharBuffer charBuf = null;
    Charset isocharset = Charset.forName("iso-8859-1");
    CharsetDecoder isoDecoder = Charset.forName("iso-8859-1").newDecoder();
    CharsetDecoder utf8Decoder = Charset.forName("UTF-8").newDecoder();
    byte sByte[] = line.getBytes("iso-8859-1");
    charBuf = utf8Decoder.decode(isoBuf);
    

    What is the easiest way to convert è­¦å: to 警告:?

    Thank You Rich

    @Pshemo had the answer I was looking for

    byte[] isoBytes = line.getBytes("ISO-8859-1");
    System.out.println(new String(isoBytes, "UTF-8"));
    

    Thank you all for your help

  • Admin
    Admin about 10 years
    Unfortunately, this is a large project and I don't have the ability to do that. I just need the quick and dirty Java code.