Je veux changer le code de caractère "A" en UTF-16
Décimal: 48 (10) 46 (10) Hexagone: 30 (16) 42 (16)
Utilisez String.Format
ou System.out.printf
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String stringValue = "Ah";
byte [] mojiCode = stringValue.getBytes(StandardCharsets.UTF_16BE);
System.out.println(mojiCode); //[B@372f7a8d
System.out.println( Arrays.toString(mojiCode)); //[48, 66]
for (Byte code : mojiCode) {
System.out.printf("%02x", code); //3042
}
System.out.println("");
StringByteArray stringByteArray = new StringByteArray(mojiCode);
System.out.println(stringByteArray.toHexString()); //3042
}
static class StringByteArray {
byte[] byteArray;
public StringByteArray(byte[] byteArray) {
this.byteArray = byteArray;
}
public String toHexString() {
StringBuffer stringBuffer = new StringBuffer();
for (byte code : byteArray) {
String hexString = String.format("%02x", code);
stringBuffer.append(hexString);
}
return stringBuffer.toString();
}
}
}
Recommended Posts