Blame view

zteits-nbiot/src/main/java/com/zteits/nbiot/decoder/Utilty.java 2.69 KB
86fff45a   zhaowg   电信IOT插件
1
2
3
4
  package com.zteits.nbiot.decoder;
  
  
  import java.util.Arrays;
be88e03a   zhaowg   电信IOT插件
5
  import java.util.Date;
86fff45a   zhaowg   电信IOT插件
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
  
  public class Utilty {
  
      private static Utilty instance = new Utilty();
  
      public static Utilty getInstance() {
          return instance;
      }
  
      public static final int MIN_MID_VALUE = 1;
  
      public static final int MAX_MID_VALUE = 65535;
  
  
      public static int bytes2Int(byte[] b, int start, int length) {
          int sum = 0;
          int end = start + length;
          for (int k = start; k < end; k++) {
              int n = ((int) b[k]) & 0xff;
              n <<= (--length) * 8;
              sum += n;
          }
          return sum;
      }
  
      public static byte[] int2Bytes(int value, int length) {
          byte[] b = new byte[length];
          for (int k = 0; k < length; k++) {
              b[length - k - 1] = (byte) ((value >> 8 * k) & 0xff);
          }
          return b;
      }
  
      public boolean isValidofMid(int mId) {
          if (mId < MIN_MID_VALUE || mId > MAX_MID_VALUE) {
              return false;
          }
          return true;
      }
  
      public static String parseByte2HexStr(byte[] buf) {
          if (null == buf) {
              return null;
          }
  
          StringBuffer sb = new StringBuffer();
          for (int i = 0; i < buf.length; i++) {
              String hex = Integer.toHexString(buf[i] & 0xFF);
              if (hex.length() == 1) {
                  hex = '0' + hex;
              }
              sb.append(hex.toUpperCase());
          }
          return sb.toString();
      }
      /**
       * 16进制转换成为integer
       * @return
       */
      public static Integer hexStringToInteger(byte[] original, int from, int to) {
          String s = Utilty.parseByte2HexStr(Arrays.copyOfRange(original, from, to));
          return Integer.parseInt(s,16);
      }
  
      /**
       * 16进制转换成为string类型字符串
       * @return
       */
      public static String hexStringToString(byte[] original, int from, int to) {
          String s = Utilty.parseByte2HexStr(Arrays.copyOfRange(original, from, to));
          if (s == null || s.equals("")) {
              return null;
          }
          s = s.replace(" ", "");
          byte[] baKeyword = new byte[s.length() / 2];
          for (int i = 0; i < baKeyword.length; i++) {
              try {
                  baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
          try {
              s = new String(baKeyword, "UTF-8");
              new String();
          } catch (Exception e1) {
              e1.printStackTrace();
          }
          return s;
      }
be88e03a   zhaowg   电信IOT插件
96
97
98
99
100
101
  
      public static void main(String[] args) {
          Date dd = new Date(1469680927*1000);
  
          System.out.println(dd);
      }
86fff45a   zhaowg   电信IOT插件
102
  }