Recently, I was updating the copyrights text in amConsole.properties.
the copyrights text contains extended ASCII characters because part
of the copyrights/license is in France.
Java Properties file cannot have extended ASCII characters, we need to
have them in Hex e.g. \u00e0 for à
Here is a small program that converted extended ASCII characters to
Hex.
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
public class StringToHex {
private static String charToHex(char c) {
StringBuffer buffer = new StringBuffer();
if (c <= 0x7E) {
buffer.append(c);
} else {
buffer.append("\\u");
String hex = Integer.toHexString(c);
for (int j = hex.length(); j < 4; j++ ) {
buffer.append('0');
}
buffer.append(hex);
}
return buffer.toString();
}
private static String readInput(String fileName) {
StringBuffer buffer = new StringBuffer();
try {
FileInputStream fis = new FileInputStream(fileName);
InputStreamReader isr = new InputStreamReader(fis, "UTF8");
Reader in = new BufferedReader(isr);
int ch;
while ((ch = in.read()) > -1) {
buffer.append(charToHex((char)ch));
}
in.close();
return buffer.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Usage: java StringToHex ");
System.exit(1);
}
String inputString = readInput(args[0]);
if (inputString != null) {
System.out.println(inputString);
}
System.exit(0);
}
}