-2

Given a file of any type, how to display its content in binary (hexadecimal) form in the console in JAVA? Suppose I have a txt file, I know how to display its contents. But if the file is a png or zip file, I need to display its content in binary (hexadecimal) form in the console.

  • http://stackoverflow.com/questions/9655181/convert-from-byte-array-to-hex-string-in-java – Amir Afghani Jul 05 '14 at 03:50
  • Read the file, "print" as hex. – Hot Licks Jul 05 '14 at 04:00
  • Thanks a lot. If I have an image file "test.png" not strings and I want to display its contents in the console in NetBeans. I cannot display the image but the binary code the image file. How can I do that? – user3204065 Jul 05 '14 at 04:01
  • So your question is really about hex display in the NetBeans editor, and not really a Java program question? I believe that there is a hex editor plug-in for NetBeans. On Unix-like systems, you can just use cat -v at the console. – Bill Jul 05 '14 at 04:10
  • This question appears to be off-topic because it lacks the efforts made by the Original Poster. Without those inputs, it is hard to tell, how much the Original Poster is aware of the topic being asked for. – nIcE cOw Jul 05 '14 at 04:52
  • 1
    Binary != hexadecimal. – user207421 Jul 05 '14 at 04:59

1 Answers1

1

Two options would be to either use String formatting:

String.format("%02x", b & 0xff) //Where b is your byte value

Or you could use your byte(s) as an integer(s) and use this method in the Integer class:

Integer.toHexString(int i)

An example:

//Just some image on my hard drive
File file = new File("C:\\Users\\%username%\\Pictures\\Memes\\What_If_I_Told_You.jpg");
StringBuilder builder = new StringBuilder();
try {
    FileInputStream fin = new FileInputStream(file);
    byte[] buffer = new byte[1024];
    int bytesRead = 0;
    while((bytesRead = fin.read(buffer)) > -1)
        for(int i = 0; i < bytesRead; i++)
            builder.append(String.format("%02x", buffer[i] & 0xFF)).append(i != bytesRead - 1 ? " " : "");
} catch (IOException e) {
    e.printStackTrace();
}
System.out.println(builder.toString());

The output of this was: ff d8 ff e0 00 10 4a 46 49 46 00 01 01 01 00 48... etc

-Thomas

T-Fowl
  • 704
  • 4
  • 9
  • Thanks a lot. If I have an image file "test.png" not strings and I want to display its contents in the console in NetBeans. I cannot display the image but the binary code the image file. How can I do that? – user3204065 Jul 05 '14 at 04:02
  • Basically you would read the contents of the file as bytes (using your standard io (or nio) mechanics); then you would take the read bytes and use one of the methods I listed to convert them into a String representing their hexadecimal values. Then just print those strings to the console. – T-Fowl Jul 05 '14 at 04:05
  • 1
    Sorry, I forgot some people are very concerned about 29,861 bytes. Though out of respect for the limited-data folk I will change it to embedded code... -_- – T-Fowl Jul 05 '14 at 05:07
  • Thanks. I'm just as concerned about legibility actually. I don't see why SO should have to become like yet another 1200-page Wrox book full of screenshots of things that can be perfectly well displayed as text. – user207421 Jul 05 '14 at 05:13
  • Okay, I understand now. I do too apologize for my hint of sarcasm. I'll keep this in mind when answering future questions. :) – T-Fowl Jul 05 '14 at 05:15