Java – Convert File to Hex
This article shows how to convert a file into a hexadecimal (hex) representative format.
for example, below is a text file.
/path/to/text.txt
ABCDEFG
12345678
!@#$%^&*()
Testing only
And we will convert the above file into the following hex format.
41 42 43 44 45 46 47 0D 0A 31 32 33 34 35 36 | ABCDEFG..123456
37 38 0D 0A 21 40 23 24 25 5E 26 2A 28 29 0D | 78..!@#$%^&*().
0A 54 65 73 74 69 6E 67 20 6F 6E 6C 79 | .Testing only
1. Java Convert File to Hex
The idea is to read the file into an InputStream
and uses String.format(%X) to convert each byte into a hex code.
FileToHex.java
package com.mkyong.io.howto;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class FileToHex {
private static final String NEW_LINE = System.lineSeparator();
private static final String UNKNOWN_CHARACTER = ".";
public static void main(String[] args) throws IOException {
String file = "/path/to/text.txt";
String s = convertFileToHex(Paths.get(file));
System.out.println(s);
}
public static String convertFileToHex(Path path) throws IOException {
if (Files.notExists(path)) {
throw new IllegalArgumentException("File not found! " + path);
}
StringBuilder result = new StringBuilder();
StringBuilder hex = new StringBuilder();
StringBuilder input = new StringBuilder();
int count = 0;
int value;
// path to inputstream....
try (InputStream inputStream = Files.newInputStream(path)) {
while ((value = inputStream.read()) != -1) {
hex.append(String.format("%02X ", value));
//If the character is unable to convert, just prints a dot "."
if (!Character.isISOControl(value)) {
input.append((char) value);
} else {
input.append(UNKNOWN_CHARACTER);
}
// After 15 bytes, reset everything for formatting purpose
if (count == 14) {
result.append(String.format("%-60s | %s%n", hex, input));
hex.setLength(0);
input.setLength(0);
count = 0;
} else {
count++;
}
}
// if the count>0, meaning there is remaining content
if (count > 0) {
result.append(String.format("%-60s | %s%n", hex, input));
}
}
return result.toString();
}
}
2. Convert Image file to Hex
Run the above program using an image file.
String file = "/path/to/hello.png";
Output
89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 44 | .PNG........IHD
52 00 00 03 11 00 00 01 1E 08 06 00 00 00 F5 | R.............õ
AE 98 9A 00 00 00 09 70 48 59 73 00 00 12 74 | ®......pHYs...t
00 00 12 74 01 DE 66 1F 78 00 00 00 07 74 49 | ...t.Þf.x....tI
//...
20 08 42 4B 88 13 21 08 82 20 08 82 20 08 42 | .BK..!.. .. .B
4B FC 7F 0B 00 ED 81 F6 3A 58 EC 00 00 00 00 | Kü...í.ö:Xì....
49 45 4E 44 AE 42 60 82 | IEND®B`.
Download Source Code
$ git clone https://github.com/mkyong/core-java
$ cd java-io/howto
This is a great example for Hex code, could you create an example to Binary, then from binary to text.
Stunning and smart code“““““““
Thanks
Thanks
Great thoughts on computers. Thrilled to have discovered your site and add it to my reader. Cheers!
Dear mkyong;
Thank! this code can help me to solve my home work. It’s very useful for improve thinking.