0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

JavaでSJISの16進からUnicodeを得る

Last updated at Posted at 2023-03-01

レガシーなシステムで文字コードを細かくチェックする機会があり、簡単なツールを作ってみました。


public class SjisToUnicode {
    public static void main( String[] args )
    {
        try {
            String sjisHex = "819d";
            byte[] sjisBytes = hexBin(sjisHex);
            String sjisStr = new String(sjisBytes, "Windows-31j");
            String unicode = strToUnicode(sjisStr);
            System.out.println(unicode);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }



    //
    public static String binHex(byte[] data) {
        StringBuffer sb = new StringBuffer();
        for (byte b : data) {
            String s = Integer.toHexString(0xff & b);
            if (s.length() == 1) {
                sb.append("0");
            }
            sb.append(s);
        }
        return sb.toString();
    }

    public static byte[] hexBin(String hex) {
        byte[] bytes = new byte[hex.length() / 2];
        for (int index = 0; index < bytes.length; index++) {
            bytes[index] = (byte) Integer.parseInt(hex.substring(index * 2, (index + 1) * 2), 16);
        }
        return bytes;
    }


    private static String strToUnicode(String original)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < original.length(); i++) {
            sb.append(String.format("\\u%04X", Character.codePointAt(original, i)));
        }
        String unicode = sb.toString();
        return unicode;
    }


    private static String unicodeToStr(String unicode)
    {
        String[] codeStrs = unicode.split("\\\\u");
        int[] codePoints = new int[codeStrs.length - 1];
        for (int i = 0; i < codePoints.length; i++) {
            codePoints[i] = Integer.parseInt(codeStrs[i + 1], 16);
        }
        String encodedText = new String(codePoints, 0, codePoints.length);
        return encodedText;
    }
}

参考記事:Java: 16進文字列とバイト列を相互変換する
参考記事:JavaのUnicode文字列の変換用メソッド("あ" <-> "\u3042")

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?