jose4ka
jose4ka

Reputation: 41

How to convert hex string to Shift JIS encoding in java?

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:

設定を初期化しますか

Upvotes: 1

Views: 485

Answers (2)

g00se
g00se

Reputation: 4296

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

will do it for you for earlier versions

Upvotes: 2

andrewJames
andrewJames

Reputation: 22020

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

Upvotes: 1

Related Questions