Date: 2011sep28
Update: 2025sep19
Language: Java
Q. Java: Convert a String into its equivalent ASCII hex characters
For example, to obfuscate a password in a config file.
A. Here's a full demo using a function called stringToHex()
class Demo {
static String stringToHex(final String in) {
StringBuilder out = new StringBuilder(in.length() * 2);
for (int i = 0; i < in.length(); i++) {
out.append(Integer.toHexString(in.charAt(i)));
}
return out.toString();
}
public static void main(String []args) {
final String out = stringToHex("secret");
System.out.println("out=" + out);
}
}
Output:
out=736563726574
The ASCII value of 's' is 73, 'e' is 65, etc