JAVA UtilTool 单片机数据处理工具实现

 
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public final class UtilTool {
	
	public static long bytes2long(byte[] bs, int off){
		long l=0;
		
    	for(int n=0;n<8;n++){
    		l += (((long)bs[off+n])& 0xFF) <<(n)*8;
    	}
    	
		return l;
	}
	
	public static int bytes2intLE(byte[] bs, int off){
		int l=0;
		
    	for(int n=0;n<4;n++){
    		l += (((int)bs[off+n])& 0xFF) <<(n)*8;
    	}
    	
		return l;
	}
	
	public static int bytes2intBE(byte[] bs, int off){
		int l=0;
		
    	for(int n=0;n<4;n++){
    		l += (((int)bs[off+n])& 0xFF) <<(3-n)*8;
    	}
    	
		return l;
	}	
	
	public static short bytes2shortBE(byte[] bs, int off){
		short l=0;
		
    	for(int n=0;n<2;n++){
    		l += (((int)bs[off+n])& 0xFF) <<(2-n)*8;
    	}
    	
		return l;
	}	
	
	public static void long2bytesBE(long i, byte[] b, int off){
    	for(int n=0;n<8;n++){
    		b[off + 3 - n] = (byte) (i >>> (n * 8));
    	}
    }	
	
	
	public static void int2bytesBE(int i, byte[] b, int off){
    	for(int n=0;n<4;n++){
    		b[off + 3 - n] = (byte) (i >>> (n * 8));
    	}
    }
	
	public static void int2bytesLE(int i, byte[] b, int off){
    	for(int n=0;n<4;n++){
    		b[off + n] = (byte) (i >>> (n * 8));
    	}
    }	
	
	public static void short2bytesBE(short i, byte[] b, int off){
    	for(int n=0;n<2;n++){
    		b[off + 1 - n] = (byte) (i >>> (n * 8));
    	}
    }
    
	public static void double2bytes(double d, byte[] b, int off){
    	long l = Double.doubleToLongBits(d);
    	for(int n=0;n<8;n++){
    		b[off + 7 - n] = (byte) (l >>> (n * 8));
    	}
    }  
	
	public static byte[] double2bytes(double d){
    	long l = Double.doubleToLongBits(d);
    	byte[] b = new byte[8];
    	for(int n=0;n<8;n++){
    		b[7 - n] = (byte) (l >>> (n * 8));
    	}
    	
    	return b;
    }	
	
	public static double bytes2doubleLSB(byte[] bs, int off){
		double d;
		long l=0;
		
    	for(int n=0;n<8;n++){
    		l += (((long)bs[off+n])& 0xFF) <<(n)*8;
    	}
		
    	d = Double.longBitsToDouble(l);  
		return d;
	}	
	
	public static double bytes2doubleMSB(byte[] bs, int off){
		double d;
		long l=0;
		
    	for(int n=0;n<8;n++){
    		l += (((long)bs[off+7-n])& 0xFF) <<(n)*8;
    	}
		
    	d = Double.longBitsToDouble(l);  
		return d;
	}	
	
	public static byte[] string2Bytes(String s){
		int cnt = s.length()/2;
		byte[] bs = new byte[cnt];
		int i = 0;;
		while(cnt-- > 0){
			bs[i] = (byte)Integer.parseInt(s.substring(i*2, i*2 + 2), 16);
			i++;
		}
		
		return bs;
	}
	
	private static final char[] HEX =
	{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
    
	public static String bytes2HEX(byte[] bytes, int start, int length) {
		if (bytes != null) {
			StringBuffer sb = new StringBuffer();
			for (int i = start; i < start + length; i++) {
				sb.append(HEX[bytes[i] >> 4 & 0x0f]);
				sb.append(HEX[bytes[i] & 0x0f]);
			}
			return sb.toString();
		} else
			throw new IllegalArgumentException("data is null");
	}	
	
	public static String bytes2HEX(byte[] bytes) {
		if (bytes != null) {
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < bytes.length; i++) {
				sb.append(HEX[bytes[i] >> 4 & 0x0f]);
				sb.append(HEX[bytes[i] & 0x0f]);
			}
			return sb.toString();
		} else
			throw new IllegalArgumentException("data is null");
	}		

	
	public static String IntToHex( int num ){
	    String sResult = "";
	    if(num == 0){
	    	sResult = "0";
	    }else{
		    while( num != 0 ){
		            int m = num % 16;
		            if ( m < 10 )
		                sResult = ( char )( '0' + m ) + sResult;
		            else
		                sResult = ( char )( 'A' + m - 10 ) + sResult;
		            num = num / 16;
		    }
	    }
	    sResult = "0x" + sResult;
	    
	    return sResult;
	}
	
	public static String trimNewLine(String str){
		int s,e;
		
		for(s=0;s=0;e--){
			if(str.charAt(e) != '\n'){
				break;
			}
		}
		
		return str.substring(s, e);
	}
	
	public static String getTime(){
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
			.format(Calendar.getInstance().getTime());
	}
	
	
	public static byte dec2Hex(byte v){
		byte rsp;
		rsp = (byte) (v%10);
		rsp += v/10*16;
		
		return rsp;
	}
	
}

你可能感兴趣的:(java,算法,开发语言)