shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
package com.walker.infrastructure.utils;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public final class ByteTools {
 
//    private Log log = LogFactory.getLog(this.getClass());
 
    /**
     * 生成指定长度的随机数字符串
     * 
     * @param intNumber
     * @return
     */
    public static String getRandomNum(int intNumber) {
        StringBuilder sb = new StringBuilder(StringUtils.EMPTY_STRING);
        Random random = new Random();
        for (int i = 0; i < intNumber; i++) {
            sb.append(String.valueOf(random.nextInt(10)));
        }
        return sb.toString();
    }
 
    private static char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A','B', 'C', 'D', 'E', 'F' };
    
    /**
     * 将一个字节以两位16进制数字表示成字符串
     * 
     * @param ib
     *            字节
     * @return 字符串,两位
     */
    public static String byteHEX(byte ib) {
//        char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A','B', 'C', 'D', 'E', 'F' };
        char[] ob = new char[2];
        ob[0] = Digit[(ib >>> 4) & 0X0F];
        ob[1] = Digit[ib & 0X0F];
        String s = new String(ob);
        return s;
    }
 
    /**
     * 2byte => 1int
     * 
     * @param byHi
     * @param byLo
     * @return 1 int
     */
    public static int byte2int(byte byHi, byte byLo) {
        return (byHi & 0xFF) << 8 | (byLo & 0xFF);
    }
 
    /**
     * 4byte => 1int
     * 
     * @param HH
     * @param HL
     * @param LH
     * @param LL
     * @return
     */
    public static int byte2int(byte HH, byte HL, byte LH, byte LL) {
        return (HH & 0xFF) << 24 | (HL & 0xFF) << 16 | (LH & 0xFF) << 8
                | (LL & 0xFF);
    }
 
    /**
     * int 转换为 byte 数组
     * 
     * @param i
     * @return
     */
    public static byte[] int2bytes(int i) {
        byte[] abyte0 = new byte[4];
        abyte0[3] = (byte) (0xff & i);
        abyte0[2] = (byte) ((0xff00 & i) >> 8);
        abyte0[1] = (byte) ((0xff0000 & i) >> 16);
        abyte0[0] = (byte) ((0xff000000 & i) >> 24);
        return abyte0;
    }
    public static byte[] int2bytes2(int i) {
        byte[] abyte0 = new byte[2];
        abyte0[1] = (byte) (0xff & i);
        abyte0[0] = (byte) ((0xff00 & i) >> 8);
        return abyte0;
    }
 
    /**
     * byte[4] 转化为int
     * 
     * @param b
     *            byte[4]
     * @return int
     */
    public static int bytes2int(byte[] b) {
        int res = 0;
        res <<= 8;
        res = res | (b[0] & 0xff);
        res <<= 8;
        res = res | (b[1] & 0xff);
        res <<= 8;
        res = res | (b[2] & 0xff);
        res <<= 8;
        res = res | (b[3] & 0xff);
        return res;
 
    }
 
    /**
     * 从一个路径得到文件名
     * 
     * @param s
     * @return
     */
    public static String getName(String s) {
        String temp = null;
        while (s.indexOf("/") > -1) {
            s = s.substring(s.indexOf("/") + 1);
        }
        temp = s;
        if (temp.indexOf(".") > -1) {
            temp = temp.substring(0, temp.indexOf("."));
        }
        return temp;
    }
 
    /**
     * 生成一个唯一的16位的字符串——16进制byte
     */
    public static byte[] randomByte() {
        String temp = null;
        temp = Long.toString(System.currentTimeMillis(), 16)
                + Integer.toString(new Random().nextInt(100000) + 100000, 16);
        return ByteTools.hexStr2byteArr(temp);
    }
 
    public static byte[] cip2bytes(String ip) {
        byte[] temp = new byte[4];
        String temps = null;
        int tempi = 0;
        byte[] tempb = new byte[4];
        for (int i = 0; i < 4; i++) {
            if (i != 3) {
                temps = ip.substring(0, ip.indexOf("."));
                ip = ip.substring(ip.indexOf(".") + 1);
                tempi = Integer.parseInt(temps);
                tempb = int2bytes(tempi);
                System.arraycopy(tempb, 3, temp, i, 1);
            } else {
                tempi = Integer.parseInt(ip);
                tempb = int2bytes(tempi);
                System.arraycopy(tempb, 3, temp, i, 1);
            }
        }
 
        return temp;
    }
 
    public static byte[] ip2bytes(String ip) {
        byte[] temp = new byte[4];
        String temps = null;
        int tempi = 0;
        byte[] tempb = new byte[4];
        for (int i = 0; i < 4; i++) {
            if (i != 3) {
                temps = ip.substring(0, ip.indexOf("."));
                ip = ip.substring(ip.indexOf(".") + 1);
                tempi = Integer.parseInt(temps);
                tempb = int2bytes(tempi);
                System.arraycopy(tempb, 3, temp, 3 - i, 1);
            } else {
                tempi = Integer.parseInt(ip);
                tempb = int2bytes(tempi);
                System.arraycopy(tempb, 3, temp, 3 - i, 1);
            }
        }
 
        return temp;
    }
 
    /**
     * 4个字节的byte[]转变为IP地址
     * */
    public static String bytes2ip(byte[] packet){
        StringBuffer sb = new StringBuffer();
        sb.append(Integer.toString(((byte) packet[0]) & 0xff));
        sb.append(".");
        sb.append(Integer.toString(((byte) packet[1]) & 0xff));
        sb.append(".");
        sb.append(Integer.toString(((byte) packet[2]) & 0xff));
        sb.append(".");
        sb.append(Integer.toString(((byte) packet[3]) & 0xff));
        
        return sb.toString();
    }
    
    /**
     * 16进制字符串转换为byte []
     * 
     * @param hex
     * @return
     */
    public static byte[] hexStr2byteArr(String hex) {
        int len = (hex.length() / 2);
        byte[] result = new byte[len];
        char[] achar = hex.toCharArray();
        for (int i = 0; i < len; i++) {
            String item = new String(achar, i * 2, 2);
            result[i] = (byte) Integer.parseInt(item, 16);
        }
        return result;
    }
 
    /**
     * 将字符串写入指定文件(当指定的父路径中文件夹不存在时,会最大限度去创建,以保证保存成功!)
     * 
     * @param res
     *            原字符串
     * @param filePath
     *            文件路径
     * @return 成功标记
     */
    public static boolean string2File(String res, String filePath) {
        boolean flag = true;
        BufferedReader bufferedReader = null;
        BufferedWriter bufferedWriter = null;
        try {
            File distFile = new File(filePath);
            if (!distFile.getParentFile().exists())
                distFile.getParentFile().mkdirs();
            bufferedReader = new BufferedReader(new StringReader(res));
            bufferedWriter = new BufferedWriter(new FileWriter(distFile));
            char buf[] = new char[1024]; // 字符缓冲区
            int len;
            while ((len = bufferedReader.read(buf)) != -1) {
                bufferedWriter.write(buf, 0, len);
            }
            bufferedWriter.flush();
            bufferedReader.close();
            bufferedWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
            flag = false;
            return flag;
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return flag;
    }
 
    /**
     * 将byte[]转化为16进制字符串
     * 
     * @param b
     *            byte[]
     * @return 16进制字符串
     */
    public static String Bytes2HexString(byte[] b) {
//        String ret = StringUtils.EMPTY_STRING;
        StringBuilder ret = new StringBuilder(StringUtils.EMPTY_STRING);
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
//                hex = '0' + hex;
                hex = Digit[0] + hex;
            }
            ret.append(hex.toUpperCase());
//            ret += hex.toUpperCase();
        }
        return ret.toString();
    }
 
    /**
     * 将高字节数组转换为int
     * 
     * @param b
     *            byte[]
     * @return int
     */
    public static int hBytesToInt(byte[] b) {
        int s = 0;
        for (int i = 0; i < 3; i++) {
            if (b[i] >= 0) {
                s = s + b[i];
            } else {
                s = s + 256 + b[i];
            }
            s = s * 256;
        }
        if (b[3] >= 0) {
            s = s + b[3];
        } else {
            s = s + 256 + b[3];
        }
        return s;
    }
    
    /**
     * 由IP形式的字符串转化为long
     * */
    public static long StringToLong(String ip) {
        long lip = 0;
        String temp = null;
        if (ip!=null && isIPv4(ip)) {
            if (ip.indexOf(".")>-1) {
                temp = ip.substring(0,ip.indexOf("."));
                ip = ip.substring(ip.indexOf(".") + 1);
                lip = lip + Long.parseLong(temp) * 1000000000;
                if (ip.indexOf(".")>-1) {
                    temp = ip.substring(0,ip.indexOf("."));
                    ip = ip.substring(ip.indexOf(".") + 1);
                    lip = lip + Long.parseLong(temp) * 1000000;
                    if (ip.indexOf(".")>-1) {
                        temp = ip.substring(0,ip.indexOf("."));
                        ip = ip.substring(ip.indexOf(".") + 1);
                        lip = lip + Long.parseLong(temp) * 1000 + Long.parseLong(ip);
                    }else {
                        return 0;
                    }
                }else {
                    return 0;
                }
            }
        }
        
        return lip;
    }
    
    /**
     * 判断字符串是否是IPv4地址
     * @param value
     * @return
     */
    public static boolean isIPv4(String value) {
        if(value == null)
            return false;
        String ip = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])" +
                "(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
        return value.matches(ip);
    }
    
    /**
     * 由long转化为IP形式的字符串
     * */
    public static String LongToString(long lip) {
        String ip = "";
        long temp = 0;
        temp = lip/1000;
        ip = String.valueOf(lip%1000);
        lip = temp/1000;
        ip = String.valueOf(temp%1000) + "." + ip;
        temp = lip/1000;
        ip = String.valueOf(lip%1000) + "." + ip;
        lip = temp/1000;
        ip = String.valueOf(temp%1000) + "." + ip;
        
        return ip;
    }
    
    /**
     * 获得主键
     * 
     * @return 主键
     */
    public static long getID() {
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        Random random = new Random();
        String id = df.format(new Date(System.currentTimeMillis()))
                + random.nextInt(1000);
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
        }
        return Long.parseLong(id);
    }
    
    public static String isIdentifier(String identifier){
        String[] Errors={"yes", "身份证号码位数不对!", "身份证号码出生日期超出范围或含有非法字符!", 
                "身份证号码校验错误!", "身份证地区非法!"};
        HashMap<String,String> area=new HashMap<String,String>();
        area.put("11","北京");
        area.put("12","天津");
        area.put("13","河北");
        area.put("14","山西");
        area.put("15","内蒙古");        
        area.put("21","辽宁");
        area.put("22","吉林");
        area.put("23","黑龙江");        
        area.put("31","上海");    
        area.put("32","江苏");
        area.put("33","浙江");
        area.put("34","安徽");
        area.put("35","福建");    
        area.put("36","江西");
        area.put("37","山东");
        area.put("41","河南");    
        area.put("42","湖北");
        area.put("43","湖南");
        area.put("44","广东");    
        area.put("45","广西");
        area.put("46","海南");
        area.put("50","重庆");
        area.put("51","四川");        
        area.put("52","贵州");
        area.put("53","云南");
        area.put("54","西藏");
        area.put("61","陕西");
        area.put("62","甘肃");
        area.put("63","青海");        
        area.put("64","宁夏");
        area.put("65","新疆");
        area.put("71","台湾");
        area.put("81","香港");
        area.put("81","香港");
        area.put("82","澳门");
        area.put("91","国外");
        
        //去掉前后空格,不能小于15位
        identifier=identifier.trim();
        if(identifier.length()<15)
            return Errors[1]; 
        //地区检验 
        if(area.get(identifier.substring(0,2))==null) 
            return Errors[4]; 
        
        String JYM; 
        String M;
        int S,Y;
        String ereg;
        Pattern pattern;
        Matcher matcher;
        //身份号码位数及格式检验 
        switch(identifier.length()){ 
        case 15:
            if ((Integer.parseInt(identifier.substring(6,8))+1900) % 4 == 0 || ((Integer.parseInt(identifier.substring(6,8))+1900) % 100 == 0 && (Integer.parseInt(identifier.substring(6,8))+1900) % 4 == 0 )){ 
                ereg="^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$";//测试出生日期的合法性 
            } 
            else { 
                ereg="^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$";//测试出生日期的合法性 
                }
            pattern = Pattern.compile(ereg);
            matcher = pattern.matcher(identifier);
            
                if(matcher.matches()) 
                    return Errors[0]; 
                else 
                    return Errors[2]; 
            //break; 
        case 18:
                //18位身份号码检测 
                //出生日期的合法性检查  
                //闰年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9])) 
                //平年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8])) 
            if ( Integer.parseInt(identifier.substring(6,8)) % 4 == 0 || (Integer.parseInt(identifier.substring(6,8)) % 100 == 0 && Integer.parseInt(identifier.substring(6,8))%4 == 0 )){ 
                ereg="^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$";//闰年出生日期的合法性正则表达式 
                } 
            else { 
                ereg="^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$";//平年出生日期的合法性正则表达式 
                } 
            pattern = Pattern.compile(ereg);
            matcher = pattern.matcher(identifier);
            
                if(matcher.matches()){//测试出生日期的合法性 
                //计算校验位 
                S = (Integer.parseInt(identifier.substring(0,1)) + Integer.parseInt(identifier.substring(10,11))) * 7 
                + (Integer.parseInt(identifier.substring(1,2)) + Integer.parseInt(identifier.substring(11,12))) * 9 
                + (Integer.parseInt(identifier.substring(2,3)) + Integer.parseInt(identifier.substring(12,13))) * 10 
                + (Integer.parseInt(identifier.substring(3,4)) + Integer.parseInt(identifier.substring(13,14))) * 5 
                + (Integer.parseInt(identifier.substring(4,5)) + Integer.parseInt(identifier.substring(14,15))) * 8 
                + (Integer.parseInt(identifier.substring(5,6)) + Integer.parseInt(identifier.substring(15,16))) * 4 
                + (Integer.parseInt(identifier.substring(6,7)) + Integer.parseInt(identifier.substring(16,17))) * 2 
                + Integer.parseInt(identifier.substring(7,8)) * 1  
                + Integer.parseInt(identifier.substring(8,9)) * 6 
                + Integer.parseInt(identifier.substring(9,10)) * 3 ; 
                Y = S % 11; 
                M = "F"; 
                JYM = "10X98765432"; 
                M = JYM.substring(Y,Y+1);//判断校验位 
                    if(M.equals(identifier.substring(17,18))) 
                        return Errors[0]; //检测ID的校验位 
                    else 
                        return Errors[3]; 
                } 
                else 
                    return Errors[2]; 
                //break; 
        default: 
                return Errors[1]; 
                //break; 
                } 
    }
}