lizhanwei
2020-03-11 a583cc69c148fb9766aa1cd728e1201d04244c57
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
package safeluck.drive.evaluation.util;
 
import android.content.res.Resources;
import android.util.Log;
import android.util.TypedValue;
 
import com.anyun.exam.lib.MyLog;
import com.anyun.exam.lib.util.ByteUtil;
import com.safeluck.aykj.utils.BytesUtils;
 
import java.util.Random;
 
/**
 * MyApplication2
 * Created by lzw on 2019/3/18. 13:13:42
 * 邮箱:632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */
public class Utils {
    private static final String TAG = "Utils";
    public static float px2dp(float value){
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,value, Resources.getSystem().getDisplayMetrics());
    }
 
    /**
     * dp值转像素
     * @param dpValue
     * @return
     */
    public static float dp2Px(int dpValue){
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX,dpValue,Resources.getSystem().getDisplayMetrics());
    }
 
    /**
     * 平台协议生成的校验码
     * @param bytes
     * @return
     */
 
    public static byte calCheckCode(byte[] bytes){
        byte checkCode =0;
        // 参与运算的两个值,如果两个相应bit位相同,则结果为0,否则为1。
        for (int i = 0; i < bytes.length; i++) {
            checkCode ^= bytes[i];
        }
 
        return checkCode;
    }
 
    /**
     * 转义
     * 采用Ox7e表示,若校验码、消息头以及消息体中出现0x7e,则要进行转义处理,转义规则定义如下:
     * 0x7e<——>0x7d后紧跟一个0x02;
     * 0x7d<——>0x7d后紧跟一个0x01。
     * @param datas
     * @return
     */
    public static byte[] transferMeaning(byte[] datas){
        byte [] temp = new byte[datas.length*2];
        int y = 0;
        temp[y++] = 0x7e;
        for (int i = 1; i < datas.length-1; i++) {
            if (datas[i] == 0x7E) {
                temp[y++] = 0x7D;
                temp[y++] = 0x02;
            } else if (datas[i] == 0x7D) {
                temp[y++] = 0x7D;
                temp[y++] = 0x01;
            } else {
                temp[y++] = datas[i];
            }
        }
        temp[y++] = 0x7e;
 
        byte[] tranferbytes = new byte[y];
        System.arraycopy(temp,0,tranferbytes,0,y);
        Log.i(TAG,"转义过后:"+ ByteUtil.byte2hex(tranferbytes));
        return tranferbytes;
    }
 
    /**
     *接收消息时:转义还原
     * @param datas
     * @return
     */
    public static byte[] parseMsg(byte[] datas){
        byte[] temp = new byte[datas.length];
        int y =0 ;
        for (int i = 0; i < datas.length; i++) {
            if (datas[i]==0x7d && datas[i+1]==0x02 ){
                temp[y++] = 0x7e;
                i++;
                continue;
            }else if (datas[i]==0x7d && datas[i+1]==0x01 ){
 
                temp[y++] = 0x7d;
                i++;
                continue;
            }else{
                temp[y++] = datas[i];
            }
        }
        return temp;
    }
 
    public static boolean isDigital(String str) {
        String regx= "^\\d+";
        return str.matches(regx);
    }
 
    public static void main(String []args){
        String str   = "EB00020000031420010000000400A5";
//        String str   = "7EEB00020000031420010000000400A5007E";
        String str1  = "EB000200000314200100000004030D00";
//        String str1  = "7EEB000200000314200100000004030D00D57E";
        String str2  = "EB000200000314200100000004035000";
        byte [] dtas=BytesUtils.hexStringToBytes(str);
        byte checkcode = calCheckCode(dtas);
        System.out.println(BytesUtils.toHexString(checkcode));
    }
 
 
 
    public static int[] getRandomInts(int size,int randomMax){
        Random random = new Random();
        int[] a=new int[size];
        int index=0;
 
        while(index<size){
            System.out.println("---------------");
            int temp=random.nextInt(randomMax);
 
            if(temp!=0&&!contains(a,temp)){
                a[index++]=temp;
            }
        }
 
        for(int i=0;i<a.length;i++){
            System.out.println(a[i]);
        }
 
 
        return a;
 
    }
 
 
 
    //该方法完成判断temp在a数组中是否包含
//包含返回true
    public static boolean contains(int[] a,int temp){
 
        for(int i=0;i<a.length;i++){
            if(a[i]==temp){
                return true;
            }
        }
 
        return false;
    }
}