How to convert Shift-JIS to UTF-8 in Java

Asked 2 years ago, Updated 2 years ago, 29 views

This is a rudimentary question.My current original file is SJIS.So, I want to display Japanese kanji on the Linux screen.
My current Java source code is:

private static String convertUTF8ToShiftJ(String uft8Stg){
    String is = null;
    String shftJStrg = null;

    try{
        byte [] bt = uft8Strg.getBytes (StandardCharsets.UTF_8);
        is = new String(bt, "SHIFT-JIS");
        shftJStr = new String(is);
        logger.info("Converted to the string:"+shftJStrg);
        System.out.println(shftJStrg);
    } catch(Exceptione){
        e.printStackTrace();
        return uft8Strg;
    }

    return shftJStrg;
}

In , the results displayed on the Linux screen are:

***UX0025.SH 開Started )
*** UX0025.SH � (executing...ec)
*** UX0025.SH 終Endedツ

However, the actual results you want to see are:

***UX0025.SH Start (started)
*** UX0025.SH Running...
*** UX0025.SH Ended

I'm in trouble because I don't know where my code is wrong.
Does anyone know?It's helpful.

java

2022-09-30 19:11

1 Answers

The proposed convertUTF8ToShiftJ() uses the Unicode string as input and encodes it into the UTF-8 byte string, forcing it to be decoded as shift JIS and returning it to Unicode.Non-ASCII characters will be corrupted.

Java String is always Unicode, so

  • Specify that the stream should be decoded as shift JIS when reading the file
  • Specify that the stream is encoded as UTF-8 during file or screen output

This should be the expected behavior.


2022-09-30 19:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.