【Java】ShiftJISの文字コード値を16進数表記で得る
String.getBytesしてから16進数形式に整形するだけ。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.UnsupportedEncodingException; | |
public class Char2SjisHexString { | |
// Test | |
public static void main(String args[]){ | |
String str = "月陽炎DVD"; | |
for (char c : str.toCharArray()) { | |
System.out.println(Character.toString(c) + ":" + char2SjisHexString(c)); | |
} | |
} | |
// Main | |
private static String char2SjisHexString(char c) { | |
try { | |
String s = Character.toString(c); | |
byte[] buf = s.getBytes("Windows-31J"); | |
String result = "0x"; | |
for (byte b : buf) { | |
result += String.format("%02x", b); | |
} | |
return result; | |
} catch (UnsupportedEncodingException e) { | |
return "エラー"; | |
} | |
} | |
} |
|
|