Код: Выделить всё
package org.example;
import java.io.*;
import java.nio.charset.StandardCharsets;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
public class Main {
public static String convertStringToHex(String str) {
char[] chars = Hex.encodeHex(str.getBytes(StandardCharsets.UTF_8));
return String.valueOf(chars);
}
public static String convertHexToString(String hex) {
String result = "";
try {
byte[] bytes = Hex.decodeHex(hex);
result = new String(bytes, StandardCharsets.UTF_8);
} catch (DecoderException e) {
throw new IllegalArgumentException("Invalid Hex format!");
}
return result;
}
public static void main(String[] args) throws IOException {
System.out.println("encoder Ü: " + convertStringToHex("Ü"));
System.out.println("decoder Ü error: " + convertHexToString("d093d19a"));
System.out.println("decoder Ü: " + convertHexToString("C39C"));
}
}
Код: Выделить всё
Output:
encoder Ü: d093d19a
decoder Ü error: Ü
decoder Ü: ?
Подробнее здесь: https://stackoverflow.com/questions/789 ... -correctly