1

I'm just wondering...is it possible to use Java to convert a .mp3 file into a text file of straight up binary (just 1's and 0's)? I figure it would involve the usage of AudioInputStream and then some method to decode individual bytes, but can someone give me an idea on where to start?

Thanks!

3 Answers3

2

You should ignore that its a MP3 file. Just treat it as a binary. Read the file byte by byte and convert each byte to its "binary" representation.

Udo Held
  • 12,314
  • 11
  • 67
  • 93
2

It's certainly possible, but you don't need to use a special audio stream. What you're looking for is to turn any type of file into a file of 1 and 0 characters. I have not tested this code, but the algorithm should be clear.

FileInputStream fin = new FileInputStream(new File(path));
BufferedWriter bw = new BufferedWriter(new FileWriter(otherpath));
byte contents[] = new byte[100];
while (fin.read(contents)!= -1)
{
  for (byte b : contents)
    for (int x = 0; x < 8; x++)
      bw.write(b>>x & 1);
}

Edit: If you just wanted to see what it looks like, you can open any kind of file with a hex editor.

Thomas
  • 5,074
  • 1
  • 16
  • 12
  • If I do this algorithm in reverse, (reading in the individual 1's and 0's, then saving it as whatever type of file I want) will it work too? –  Feb 17 '12 at 22:36
  • If you try a naive implementation, no. Notice that it splits the byte into 8 pieces to print 1s and 0s. They would need to be recombined before that byte was written back to a file. Something like 'read 8 characters -> convert to 1 byte -> write to file' would do. If that's the task you want to do though, use an existing solution like [HxD](http://mh-nexus.de/en/hxd/) – Thomas Feb 17 '12 at 22:40
1

Open command prompt and type xxd -b filename.mp3

arjun rj
  • 11
  • 1