当前位置 : 主页 > 编程语言 > java >

hex和mot转bin

来源:互联网 收集:自由互联 发布时间:2021-06-28
gistfile1.txt import java.io.*;import java.util.zip.CRC32;public class Hex_Mot2Bin {public void fileInmethod(File fl) throws IOException{BufferedReader br=new BufferedReader(new FileReader(fl));String fileName=fl.getAbsolutePath();String la
gistfile1.txt
import java.io.*;
import java.util.zip.CRC32;
public class Hex_Mot2Bin {	
	public void fileInmethod(File fl) throws IOException{
		BufferedReader br=new BufferedReader(new FileReader(fl));
		String fileName=fl.getAbsolutePath();
		String lastName=fileName.substring(fileName.length()-3);
		String fileData=null;
		if(lastName.equals("hex")){
			fileData=hex2Bin(br);
			byte[]b=hexStringToBytes(fileData);
			createFile(b,fileName);
		}else if(lastName.equals("mot")){
			fileData=mot2Bin(br);
			byte[]b=hexStringToBytes(fileData);
			createFile(b,fileName);
		}else if(lastName.equals("bin")){
			InputStream ips=new FileInputStream(fl);
			byte[] filebin=new byte[(int)fl.length()];
			ips.read(filebin);
			byte addStart=0;
			long filelen=fl.length();            
			byte[]bnull=new byte[20];
			bnull[0] = (byte)((addStart / 65536) / 256);
            bnull[1] = (byte)((addStart/ 65536) % 256);
            bnull[2] = (byte)((addStart % 65536) / 256);
            bnull[3] = (byte)((addStart % 65536) % 256);
            bnull[4] = (byte)((filelen / 65536) / 256);
            bnull[5] = (byte)((filelen / 65536) % 256);
            bnull[6] = (byte)((filelen % 65536) / 256);
            bnull[7] = (byte)((filelen % 65536) % 256);		
			String newFile=fl.getAbsolutePath()+"bin";
			OutputStream ops=new FileOutputStream(newFile);
			byte[] bt=crc32(filebin);
			bnull[8]=bt[0];
			 bnull[9]=bt[1];
			 bnull[10]=bt[2];
			 bnull[11]=bt[3];
			ops.write(bnull);//���ֽ�д���ļ�ͷ
			ops.write(filebin);
			ops.flush();		
			ops.close();				
			ips.close();
		}
	}
	//2017-5-16,添加算法加到文件头第9字节到12字节(共4字节)
	public static byte[] crc32(byte[] data) {
		CRC32 crc32 = new CRC32();
		crc32.update(data);
		long value = crc32.getValue();
		byte[] src = new byte[4];
		src[0] = (byte) ((value >> 24) & 0xFF);
		src[1] = (byte) ((value >> 16) & 0xFF);
		src[2] = (byte) ((value >> 8) & 0xFF);
		src[3] = (byte) (value & 0xFF);
		return src;
	}
	
	//�ļ�����
		private long addLong=0;//�׵�ַȡ����04��ַ��������ݵ�ַ
	 private void createFile(byte[] filebin,String filename) throws IOException{		
		 //byte[]bnull=new byte[]{0,62,-128,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0};//���20�����ֽ�
		 byte[]bnull=new byte[]{66,48,48,48,70,70,10,0,0,0,0,0,0,0,0,0,0,0,0,0};
		 bnull[0] = (byte)((addLong / 65536) / 256);
		 bnull[1] = (byte)((addLong / 65536) % 256);
		 bnull[2] = (byte)((addLong % 65536) / 256);
		 bnull[3] = (byte)((addLong % 65536) % 256);
		 bnull[4] = (byte)((filebin.length / 65536) / 256);
		 bnull[5] = (byte)((filebin.length / 65536) % 256);
		 bnull[6] = (byte)((filebin.length % 65536) / 256);
		 bnull[7] = (byte)((filebin.length % 65536) % 256);	
		 byte[] newAdd=crc32(filebin);
		 bnull[8]=newAdd[0];
		 bnull[9]=newAdd[1];
		 bnull[10]=newAdd[2];
		 bnull[11]=newAdd[3];
			String newFile=filename+"bin";
			OutputStream ops=new FileOutputStream(newFile);			
			ops.write(bnull);
			ops.write(filebin);			
			ops.flush();		
			ops.close();
	 }
	 //���ڴ��ַ��ֵ
	 private int differ(int addNum,int temp){
			if(addNum>temp){return addNum-temp;}
			else{
				return 65536+addNum-temp;
				}
		}
	 //�ڴ��ַתint
		private int add2Int(String s){
			if(s.length()>0){
				byte[]b=hexStringToBytes(s);
				return  byteArrayToInt(b); 			
			}else{
				return 0;
			}
		}
		//�ڴ��ַbyte����תint
		public int byteArrayToInt(byte[] b) {  
			   if(b.length==2){
				return   b[1] & 0xFF |  
			            (b[0] & 0xFF) << 8 ;  
			   }if(b.length==3){
				   return   (int) ((b[2]&0xFF)   
				            | ((b[1]<<8) & 0xFF00)  
				            | ((b[0]<<16)& 0xFF0000)); 
			   }if(b.length==4){
				   return  (int) ((b[3]&0xFF)   
				            | ((b[2]<<8) & 0xFF00)  
				            | ((b[1]<<16)& 0xFF0000)   
				            | ((b[0]<<24) & 0xFF000000));
			   }else{
				   return b[0];
			   }
			}
		//�ַ�ת�ֽ�����
		private byte[] hexStringToBytes(String hexString) {  			   
			    hexString = hexString.toUpperCase();  
			    int length = hexString.length() / 2;  
			    char[] hexChars = hexString.toCharArray();  
			    byte[] d = new byte[length];  
			    for (int i = 0; i < length; i++) {  
			        int pos = i * 2;  
			        d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));  
			    }  
			    return d;  
			}
		//�����ַ�ת���飨��תchar��
		private byte charToByte(char c) {  
		    return (byte) "0123456789ABCDEF".indexOf(c);  
		}
		/** 
		 * hexתbin
		 * �ļ�ͷ��ַ=���Ե�ַ+������ݵ�ַ
		 */	
	public String hex2Bin(BufferedReader br) throws IOException{
		String line=null;		//��ȡ�������ַ�
		String strData=null;	//�ַ�����Ҫ��������
		StringBuffer sbf=new StringBuffer();
		int addNum=0;		//�����ڴ��ַ
		int temp=0;			//���е�ַ��
		int dataleng=0;		//��һ����ݳ���
		int nulAdd=0;		//�ڴ��ַ
		boolean bl=true;	//������ֻ�ڶ�ȡ�ļ�ͷʱִ��һ��
		String firstAdd=null;	//ȡ20�ֽڵ�ַͷ	
		
		while((line=br.readLine())!=null){						
			if(!line.substring(0, 1).equals(":")){	//��:��ʼ
				break;
			}
			//addLong+=add2Int(line.substring(3, 7));
			if(line.substring(7, 9).equals("04")||line.substring(7, 9).equals("02")){
				firstAdd=line.substring(9, 13);								
			}			
			else if(line.substring(7, 9).equals("00")){//��ȡ��ݲ���			
				addNum=add2Int(line.substring(3, 7));	//��ȡ��ַ����
				
				strData=line.substring(9, line.length()-2);	//��ȡÿ�����							
				if(temp!=0){															
					nulAdd=differ(addNum,temp)-(dataleng/4);	//��ַ��ֵ����ǰ���ֽ���ȱ���ֽ��������ǰ����															
					temp=addNum;					
					if(nulAdd>0){								
						for(int j=0;j
 
  0){						
						for(int j=0;j
  
   0){ for(int j=0;j
   
    0){ for(int j=0;j
   
  
 
上一篇:分享到微信
下一篇:两个集合笛卡尔积
网友评论