Jump to content

BinToFile <=> FileToBin

- - - - -

  • Please log in to reply
No replies to this topic

#1
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts
Small code for those who might need it...
From bin to file...
import java.io.*;

public class BinToFile {

	public static void main(String[] arg) {

		String file = "bytefil.dat";

		try {

			FileOutputStream fout = new FileOutputStream(file);

			int i = 55;

			char c = '+';

			byte b = 127;

			byte[] ba = {1,2,3,4};

			

			fout.write(2);

			fout.write(i);

			fout.write('A');

			fout.write(c);

			fout.write(b);

			fout.write(ba);

			fout.close();

		}catch(IOException e) {

			System.out.println(e);

		}

	}

}
From file to bin
import java.io.*;

public class FileToBin {	

	public static void main(String[] arg) {

		String file = "bytefil.dat";

		

		try {

			FileInputStream fin = new FileInputStream(file);

			int i = 0;

			i = fin.read();

			while(i!=-1) {

				System.out.print(i+" ");

				System.out.println((char)i);

				i=fin.read();	

			}

			fin.close();

		}catch(IOException e) {

			System.out.println(e);

		}

	}

}

Posted Image




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users