Скачиваний:
21
Добавлен:
10.02.2020
Размер:
2 Кб
Скачать
import javax.swing.*;
import java.awt.*;
import java.lang.String.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;

public class Hex_code {
	static String Hex_code(String bytes) {
		
		 StringBuffer sb = new StringBuffer(bytes);
		 if(sb.length()>8) {
		 while(sb.length()>8) {
	      sb.delete(0,1);} }
	int decimal = Integer.parseInt(sb.toString(),2);
	String hexStr = Integer.toString(decimal,16);
	return hexStr;
	}
	
	static String Hex_file(File file1,JProgressBar progressBar) throws IOException {
		progressBar.setValue(30);
		System.out.println("Назва файлу = " + file1.getName());
		StringBuilder sb = new StringBuilder();
		StringBuffer strBuffer = new StringBuffer();
		StringBuffer strBuffer1 = new StringBuffer();
		strBuffer.setLength(0);
		strBuffer1.setLength(0);
		int s=0;
		try(FileInputStream fin=new FileInputStream(file1))
		{
            byte[] buffer = new byte[fin.available()];
            fin.read(buffer, 0, buffer.length);
            for (int i = 0; i < buffer.length; i++) {
            	String digit = Hex_code(Integer.toBinaryString((int) buffer[i]));
            	if (digit.length()==1) { strBuffer.append("0"); strBuffer1.append("0");}
            	strBuffer.append(digit);
            	strBuffer1.append(digit);
            	strBuffer.append(" ");
            	strBuffer1.append(" ");
            	s++;
            	if (s%16==0)strBuffer.append("\n");
            }
            progressBar.setValue(50);
            //System.out.print(strBuffer.toString());
        }
        catch(IOException ex){
             
            System.out.println(ex.getMessage());
        }
		
		try(FileWriter writer = new FileWriter("Hex1.txt", false))
        {
            writer.write(strBuffer.toString());
            writer.flush();
            progressBar.setValue(60);
        }
        catch(IOException ex){
             
            System.out.println(ex.getMessage());
        } 
		FileWriter writer = new FileWriter("Hex.txt", false);
          writer.write(strBuffer1.toString());
          writer.flush();
          
          return strBuffer.toString();
	}
}
Соседние файлы в папке src