ConvertUtil.java
5.89 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package com.bsth.util;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ConvertUtil {
private final static Logger log = LoggerFactory.getLogger(ConvertUtil.class);
private static int parse(char c) {
if (c >= 'a') {
return c - 'a' + 10 & 0xF;
}
if (c >= 'A') {
return c - 'A' + 10 & 0xF;
}
return c - '0' & 0xF;
}
public static String bcd2string(byte[] bytes, int start, int len) {
String temp = "";
for (int i = 0;i < len;i++) {
int val = bytes[start + i] & 0xff;
if (val < 10) temp += 0;
temp += Integer.toHexString(val);
}
return temp;
}
public static byte[] string2bcd(String hexstr) {
int len = hexstr.length() % 2 == 0 ? hexstr.length() / 2 : hexstr.length() / 2 + 1;
byte[] b = new byte[len];
int j = 0;
for (int i = 0; i < b.length; i++) {
char c0 = hexstr.charAt(j++);
char c1 = hexstr.charAt(j++);
b[i] = ((byte) (parse(c0) << 4 | parse(c1)));
}
return b;
}
public static int bcd2int(byte[] bytes, int start, int len) {
if (len > 4 || len == 0)
throw new IllegalArgumentException("bcd2int 字节数不符合要求");
String temp = "";
for (int i = 0;i < len;i++) {
int val = bytes[start + i] & 0xff;
if (val < 10) temp += 0;
temp += Integer.toHexString(val);
}
return Integer.parseInt(temp);
}
public static byte[] int2bcd(int val, int len) {
if (len > 4 || len == 0)
throw new IllegalArgumentException("int2bcd 字节数不符合要求");
byte[] result = new byte[len];
for (int i = len - 1;i > -1;i--) {
result[i] = (byte)Integer.parseInt(val%100 + "", 16);
val = val/100;
}
return result;
}
public static long bcd2long(byte[] bytes, int start, int len) {
if (len > 8 || len == 0)
throw new IllegalArgumentException("bcd2long 字节数不符合要求");
String temp = "";
for (int i = 0;i < len;i++) {
int val = bytes[start + i] & 0xff;
if (val < 10) temp += 0;
temp += Integer.toHexString(val);
}
return Long.parseLong(temp);
}
public static byte[] long2bcd(long val, int len) {
if (len > 8 || len == 0)
throw new IllegalArgumentException("long2bcd 字节数不符合要求");
byte[] result = new byte[len];
for (int i = len - 1;i > -1;i--) {
result[i] = (byte)Integer.parseInt(val%100 + "", 16);
val = val/100;
}
return result;
}
public static String ascii2string(byte[] bytes, int start, int len) {
return new String(Arrays.copyOfRange(bytes, start, start + len));
}
public static byte[] string2ascii(String val, int len) {
byte[] result = new byte[len];
byte[] bytes = val.getBytes();
if (bytes.length > len && len != 0)
throw new IllegalArgumentException("string2ascii 参数不符合要求");
if (len != 0)
for (int i = 0, l = bytes.length;i < l;i++) result[i] = bytes[i];
else return bytes;
return result;
}
public static long bcd2timestamp(byte[] bytes, int start) {
String temp = "20";
for (int i = 0;i < 6;i++) {
int val = bytes[start + i] & 0xff;
if (val < 10) temp += 0;
temp += Integer.toHexString(val);
}
Date date = null;
try {
date = DateTime.parse(temp, DateTimeFormat.forPattern("yyyyMMddHHmmss")).toDate();
} catch (Exception e) {
log.error("协议中时间数据异常:" + temp);
}
if (date != null) return date.getTime();
return -1;
}
public static byte[] timestamp2bcd(long val) {
byte[] result = new byte[6];
Calendar c = Calendar.getInstance();
c.setTime(new Date(val));
result[0] = (byte)(Integer.parseInt(c.get(Calendar.YEAR)%100 + "", 16));
result[1] = (byte)(Integer.parseInt(c.get(Calendar.MONTH) + 1 + "", 16));
result[2] = (byte)Integer.parseInt(c.get(Calendar.DAY_OF_MONTH) + "", 16);
result[3] = (byte)Integer.parseInt(c.get(Calendar.HOUR_OF_DAY) + "", 16);
result[4] = (byte)Integer.parseInt(c.get(Calendar.MINUTE) + "", 16);
result[5] = (byte)Integer.parseInt(c.get(Calendar.SECOND) + "", 16);
return result;
}
public static byte[] int2hex(int val, int len) {
if (len < 1)
throw new IllegalArgumentException("int2hex len 参数不符合要求");
byte[] result = new byte[len];
for (int i = 0;i < len;i++) {
result[len - 1 - i] = (byte)(val%100);
val = val/100;
}
return result;
}
public static long bytes2timestamp(byte[] bytes, int start) {
StringBuilder temp = new StringBuilder("20");
for (int i = 0;i < 6;i++) {
int val = bytes[start + i] & 0xff;
if (val < 10) { temp.append(0); }
temp.append(val);
}
Date date = null;
try {
date = DateTime.parse(temp.toString(), DateTimeFormat.forPattern("yyyyMMddHHmmss")).toDate();
} catch (Exception e) {
log.error("协议中时间数据异常:" + temp);
}
if (date != null) { return date.getTime(); }
return -1;
}
public static byte[] timestamp2bytes(long val) {
byte[] result = new byte[6];
Calendar c = Calendar.getInstance();
c.setTime(new Date(val));
result[0] = (byte)(c.get(Calendar.YEAR)%100);
result[1] = (byte)(c.get(Calendar.MONTH) + 1);
result[2] = (byte)c.get(Calendar.DAY_OF_MONTH);
result[3] = (byte)c.get(Calendar.HOUR_OF_DAY);
result[4] = (byte)c.get(Calendar.MINUTE);
result[5] = (byte)c.get(Calendar.SECOND);
return result;
}
public static int bytes2int(byte[] bytes, int start, int len) {
if (len > 4) { throw new IllegalArgumentException("bytes2int len 参数不符合要求"); }
if (len == 0) { len = 4; }
int result = 0;
for (int i = 0;i < len;i++) {
result += (bytes[start + i] & 0xff) << (len - i - 1) * 8;
}
return result;
}
public static String toHexString(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("0x%02x ", b));
}
return sb.toString();
}
public static void main(String args[]) {
for (byte b : int2hex(2231605, 4)){
System.out.println(b);
}
}
}