1

How can I convert a word's HEX code string to Shift JIS encoding?

For example, I have a string:

"90DD92E882F08F898AFA89BB82B582DC82B782A9"

And I want to get the following output:

設定を初期化しますか

jose4ka
  • 11
  • 1
  • 1
    Use [`HexFormat.of().parseHex(...)`](https://stackoverflow.com/a/11208685/12567365) followed by [`String s2 = new String(bytes, "Shift_JIS");`](https://stackoverflow.com/a/13990982/12567365). Assumes you have Java 17+ for the first step. – andrewJames Aug 15 '22 at 13:00
  • @andrewJames – I have not checked it, but assuming that it is correct, you should issue it as an answer. – tquadrat Aug 15 '22 at 13:04

2 Answers2

2
String s = new String(new BigInteger("90DD92E882F08F898AFA89BB82B582DC82B782A9", 16).toByteArray(), "Shift_JIS");

will do it for you for earlier versions

g00se
  • 3,207
  • 2
  • 5
  • 9
1

Assuming you have Java 17+, which added java.util.HexFormat, then you can use parseHex followed by a conversion from the byte array to a string:

byte[] bytes = HexFormat.of().parseHex("90DD92E882F08F898AFA89BB82B582DC82B782A9");
String str = new String(bytes, "Shift_JIS");

If you do not have Java 17+, then the related answer I linked to gives an alternative approach instead of parseHex.

I don't have the correct charset/font to show the result in my console, but here is the str variable in my debugger:

enter image description here

andrewJames
  • 19,570
  • 8
  • 19
  • 51