3

If suppose I have a file such as music.mp3 or image.jpg or any file to be considered. How do i get the binary representation of those files to a string value in java so that i can view it ??

Eg: Suppose I have file called 'video.mp4' then it must be represented as 101011010101000111010 or something like that in memory i need to get the value and store it in String like String s = "101011010101000111010"

Does anyone have any idea on how to do this in java ??

EDIT 2 How can i convert back the string to the actual file ??

Girish Nair
  • 5,148
  • 5
  • 40
  • 61
  • 4
    First [read the file into array](http://stackoverflow.com/questions/7250229/reading-a-binary-file-into-a-single-byte-array-in-java) then [display the bytes, one by one, as binary](http://stackoverflow.com/questions/5263187/how-can-i-print-a-integer-in-binary-format-in-java) – Dariusz Jan 18 '13 at 08:50
  • You really want it in binary? Not in hexadecimal? – Jim Garrison Jan 18 '13 at 08:50
  • read here.. it's the same question .. http://stackoverflow.com/questions/7119141/java-file-to-binary-conversion – Elior Jan 18 '13 at 08:57

1 Answers1

5

I would suggest hexadecimal, it's much more compact and readable

    StringBuilder sb = new StringBuilder();
    try (BufferedInputStream is = new BufferedInputStream(new FileInputStream("1.txt"))) {
        for (int b; (b = is.read()) != -1;) {
            String s = Integer.toHexString(b).toUpperCase();
            if (s.length() == 1) {
                sb.append('0');
            }
            sb.append(s).append(' ');
        }
    }
    System.out.println(sb);

output

71 77 65 77 65 72 0D 0A 71 77 72 65 0D 0A 72 77 65 72 0D 0A 

for binary string change the code

            String s = "0000000" + Integer.toBinaryString(b);
            s = s.substring(s.length() - 8); 
            sb.append(s).append(' ');

and get this output

01110001 01110111 01100101 01110111 01100101 01110010 00001101 00001010 

and this is how to parse the string and write back to a file

    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("2.txt"));
    Scanner sc = new Scanner(s);
    while (sc.hasNextInt()) {
        int b = sc.nextInt(2);
        out.write(b);
    }
    out.close();
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • 1
    That's not the answer to Grish's question. He asked for binary output, why write hexadecimal version? I also think that it is much more readable, but the whole concept of stackoverflow (i think) is to answer questions as preceisely as possible. – Dariusz Jan 18 '13 at 10:12
  • @Evgeniy Dorofeev: How can i convert back the string to the actual file ?? – Girish Nair Jan 21 '13 at 04:52
  • @Evgeniy Dorofeev: I used the `FileWriter` to create the file using `Scanner sc = new Scanner(sb.toString());` but i am getting a 0kb file ?? Can you explain how to write it ?? – Girish Nair Jan 21 '13 at 06:13
  • See my update. It parses "01110001 01110111 ..." and writes it byte by byte to 2.txt – Evgeniy Dorofeev Jan 21 '13 at 06:19
  • 1
    @EvgeniyDorofeev How would I go about writing the hex values back to a file? Above you showed how to do it if we are putting the binary values to a new file, but how do we do it with hex? – frosty Nov 23 '14 at 03:13