I am reading a text file in Java line by line. For debugging purpose, I would like to get the hex value of the string to check how it is encoded.
For example:
This is my text-file:
äpple
tree
This is how I read the file:
inputCharset = Charset.forName("ISO-8859-1")
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fi), inputCharset));
while ( in.ready() ) {
String row = in.readLine();
System.out.println(row + " = " + toHex(row));
}
The result must be for first line
äpple = E4 70 70 6C 65
How can I convert the chars of the string to the appropriate hex value?
In order to get the binary representation of the string, you use the
getBytes
method on the string:Once you have a raw byte array, there are various ways to display it as a hex string.