LoginSignup
0
0

[Java]MS932をUTF8にエンコードしなおす

Last updated at Posted at 2024-02-25

ユースケース

IDEでデフォルト・エンコードがMS932になってる際に、無理やりUTF8の文字列を用意したい。

コード

MS932文字列をMS932方式でバイトに分解し、そのバイト文字列をUTF8方式で文字列に組み替える。

public String convertToUTF8(String ms932String){
  String result = "";
    
  try {
    String tempString = new String(ms932String.getBytes("MS932"), "MS932");
    String encodedUtf8 = new String(tempString.getBytes("UTF-8"), "UTF-8");
    result = encodedUtf8;
  } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
  }

  return result;
}

なんか他にいい方法ないかなあ?
最初からUTF8で全部やりたい

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