Conversion from string to hex: First encode the unicode string into some binary format, I recommend utf-8, utf-16, or utf-32 that are not lossy. utf-8 is the best if you are working with string that contain mostly latin characters. Then you convert the byte array to a hex string.
Conversion from hex to string: Convert the hex string into a byte array, and then you can easily convert this byte array to the original string if you know what the encoding is.
HexEncode.java:
import java.io.UnsupportedEncodingException;
public class HexEncode {
static class BadInputStringException extends Exception {
public BadInputStringException(String arg0) {
super(arg0);
}
}
private static String ENCODING = "utf-8";
static private final char[] HEX_DIGITS = new char[] {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
static private char intToHexDigit(int b) {
assert b>=0 && b<16;
return HEX_DIGITS[b];
}
static private int hexDigitToInt(char hexDigit) throws BadInputStringException {
if (hexDigit>='0' && hexDigit<='9')
return (int)(hexDigit - '0');
if (hexDigit>='a' && hexDigit<='f')
return (int)(hexDigit - 'a' + 10);
if (hexDigit>='A' && hexDigit<='F')
return (int)(hexDigit - 'A' + 10);
throw new BadInputStringException("Invaid hex digit: " + hexDigit);
}
private String asciiToHex(String ascii) throws UnsupportedEncodingException, BadInputStringException {
byte[] encoded = ascii.getBytes(ENCODING);
StringBuilder sb = new StringBuilder();
for (int i=0; i<encoded.length; i++) {
byte b = encoded[i];
sb.append(intToHexDigit((b >> 4) & 0xF));
sb.append(intToHexDigit(b & 0xF));
}
return sb.toString();
}
private String hextoAscii(String hex) throws UnsupportedEncodingException, BadInputStringException {
if (0 != (hex.length() & 1))
throw new BadInputStringException("The hex string must contain even number of digits!");
int encoded_len = hex.length() / 2;
byte[] encoded = new byte[encoded_len];
for (int i=0; i<encoded_len; i++) {
encoded[i] = (byte)((hexDigitToInt(hex.charAt(i*2)) << 4) | hexDigitToInt(hex.charAt(i*2+1)));
}
return new String(encoded, ENCODING);
}
private void run() {
try {
String hex = asciiToHex("TRON");
String ascii = hextoAscii(hex);
System.out.printf("hex: %s, decoded_hex: %s", hex, ascii);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
new HexEncode().run();
}
}