Blame view

src/cn/pda/serialport/Tools.java 1.63 KB
c05c7a9b   黄灿宏   标准版本 1.扩展设备8 2....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  package cn.pda.serialport;
  
  import java.text.SimpleDateFormat;
  
  
  public class Tools {
  
  	//byte 转十六进制
  	public static String Bytes2HexString(byte[] b, int size) {
  		String ret = "";
  		for (int i = 0; i < size; i++) {
  			String hex = Integer.toHexString(b[i] & 0xFF);
  			if (hex.length() == 1) {
  				hex = "0" + hex;
  			}
  			ret += hex.toUpperCase();
  		}
  		return ret;
  	}
  
  	public static byte uniteBytes(byte src0, byte src1) {
  		byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue();
  		_b0 = (byte)(_b0 << 4);
  		byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue();
  		byte ret = (byte)(_b0 ^ _b1);
  		return ret;
  	}
  
  	//十六进制转byte
  	public static byte[] HexString2Bytes(String src) {
  		int len = src.length() / 2;
  		byte[] ret = new byte[len];
  		byte[] tmp = src.getBytes();
  
  		for (int i = 0; i < len; i++) {
  			ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
  		}
  		return ret;
  	}
  
  	/* byte[]转Int */
  	public static int bytesToInt(byte[] bytes)
  	{
  		int addr = bytes[0] & 0xFF;
  		addr |= ((bytes[1] << 8) & 0xFF00);
  		addr |= ((bytes[2] << 16) & 0xFF0000);
  		addr |= ((bytes[3] << 25) & 0xFF000000);
  		return addr;
  
  	}
  
  	/* Int转byte[] */
  	public static byte[] intToByte(int i)
  	{
  		byte[] abyte0 = new byte[4];
  		abyte0[0] = (byte) (0xff & i);
  		abyte0[1] = (byte) ((0xff00 & i) >> 8);
  		abyte0[2] = (byte) ((0xff0000 & i) >> 16);
  		abyte0[3] = (byte) ((0xff000000 & i) >> 24);
  		return abyte0;
  	}
  
  	public static String getmyTime() {
  		SimpleDateFormat sDateFormat    =   new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  		String date    =    sDateFormat.format(new    java.util.Date());
  		return date;
  	}
  
  
  
  
  }