Skip to content

Instantly share code, notes, and snippets.

@dev-jonghoonpark
Last active June 11, 2018 04:56
Show Gist options
  • Save dev-jonghoonpark/00cd92040f4507c8f8dab1c12ff3d51d to your computer and use it in GitHub Desktop.
Save dev-jonghoonpark/00cd92040f4507c8f8dab1c12ff3d51d to your computer and use it in GitHub Desktop.
print file byte array to hex
import org.apache.commons.io.FileUtils;
import java.io.File;
public class MainClass {
private final static char[] hexArray = "0123456789ABCDEF".toCharArray();
public static void main(String[] args) {
File fileDirectory = new File("files");
for (File file : fileDirectory.listFiles()) {
try {
byte[] byteArray = new byte[(int) file.length()];
byteArray = FileUtils.readFileToByteArray(file);
System.out.println(bytesToHex(byteArray));
} catch (Exception e) {
e.printStackTrace();
}
}
}
// method from : https://stackoverflow.com/a/9855338/5138078
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>chooco13.io</groupId>
<artifactId>byte-based-extension-finder</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.22</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment