左晓为主开发手持机充值管理机
zuoxiao
2024-09-27 feb7add1979c830e0d940286242a7c489f2f8f96
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
package com.dayu.henanlibrary.utils;
 
 
import android.content.Context;
import android.os.Handler;
import android.os.Message;
 
import com.dayu.baselibrary.tools.HexUtil;
import com.dayu.henanlibrary.dao.HNBaseDaoSingleton;
import com.dayu.henanlibrary.dbBean.AdminDataBean;
 
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Random;
import java.util.regex.Pattern;
 
 
/**
 * Copyright (C), 2022,
 * Author: zuo
 * Date: 2022/2/9 10:12
 * Description:
 */
public class SocketUtil {
    //保存当前到了第几个订单了
    public static final String OrderNo = "OrderNo";
 
 
    /**
     * 字符串转换成为16进制(无需Unicode编码)
     *
     * @param str
     * @return
     */
    public static String str2HexStr(String str) {
        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = str.getBytes();
        int bit;
        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
            // sb.append(' ');
        }
        return sb.toString().trim();
    }
 
 
    /**
     * 16进制直接转换成为字符串
     *
     * @param hexString 16进制字符串
     * @return String (字符集:UTF-8)
     * @explain
     */
    public static String fromHexString(String hexString) {
        // 用于接收转换结果
        String result = "";
        // 转大写
        hexString = hexString.toUpperCase();
        // 16进制字符
        String hexDigital = "0123456789ABCDEF";
        // 将16进制字符串转换成char数组
        char[] hexs = hexString.toCharArray();
        // 能被16整除,肯定可以被2整除
        byte[] bytes = new byte[hexString.length() / 2];
        int n;
 
        for (int i = 0; i < bytes.length; i++) {
            n = hexDigital.indexOf(hexs[2 * i]) * 16 + hexDigital.indexOf(hexs[2 * i + 1]);
            bytes[i] = (byte) (n & 0xff);
        }
        // byte[]-->String
        try {
            result = new String(bytes, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }
 
    /**
     * @param hex
     * @return
     */
    public static int get16to10(String hex) {
        int x = 0;
        try {
            x = Integer.parseInt(hex, 16);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return x;
    }
 
    /**
     * 16进制转10进制带正负
     *
     * @param hexString
     * @return
     */
    public static int hexToDecimal(String hexString) {
        // 移除可能存在的负号
        hexString = hexString.replace("-", "");
 
        // 将16进制字符串转换为BigInteger对象
        BigInteger bigInteger = new BigInteger(hexString, 16);
 
        // 将BigInteger对象转换为int类型
        int decimalValue = bigInteger.intValue();
 
        // 如果原始字符串有负号,则将结果取反
        if (hexString.startsWith("-")) {
            decimalValue = -decimalValue;
        }
 
        return decimalValue;
    }
 
 
    /**
     * 获取充值管理机序列号
     *
     * @param context
     * @return
     */
    public static String getXuLie(Context context) {
 
        String xuLie = "";
        try {
            AdminDataBean villageNo = HNBaseDaoSingleton.getInstance(context).adminDao().findFirst();
            if (villageNo != null) {
                xuLie = SocketUtil.get10to16LowHigh(Integer.valueOf(villageNo.getSerial()), 6);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return xuLie;
    }
 
 
    /**
     * 十六进制数隔空位
     *
     * @param str
     * @return
     */
    private static String spaceHighHex(String str) {
        char[] array = str.toCharArray();
        if (str.length() <= 2) return str;
        StringBuffer buffer = new StringBuffer();
        if (str.length() % 2 == 0) {
            for (int i = 0; i < array.length; i++) {
                int start = i + 1;
                if (start % 2 == 0) {
                    buffer.append(array[i]).append(" ");
                } else {
                    buffer.append(array[i]);
                }
            }
        } else {
            for (int i = 0; i < array.length; i++) {
                if (i == 0) {
                    buffer.append(array[i]).append(" ");
                } else if (i % 2 == 0) {
                    buffer.append(array[i]).append(" ");
                } else {
                    buffer.append(array[i]);
                }
            }
        }
        return buffer.toString();
    }
 
 
    /**
     * 十进制转16进制低位在前高位在后
     *
     * @param number 十进制数
     * @param length 补足多少位
     * @return
     */
    public static String get10to16LowHigh(int number, int length) {
        String str = "";
        try {
            str = Integer.toHexString(number);
            str = getHexToLenght(str, length);
            str = HexUtil.spaceHex(str);
            str = HexUtil.HighLowHex(str);
 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }
 
    /**
     * 低位在前高位在后16进制转10进制
     *
     * @param hexNumber 十进制数
     * @return
     */
    public static int get16to10LowHigh(byte[] hexNumber) {
 
        int number = 0;
        try {
            String str = HexUtil.bytesToHex(hexNumber);
            str = HexUtil.spaceHex(str);
            str = HexUtil.HighLowHex(str);
            number = hexToDecimal(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return number;
    }
 
 
    public static int hexadecimal16Conversion(String hexadecimalStr) {
        int getDataDecimal = 0;//转化得到的目标数据
//16进制代表数据 4位数字
        if (hexadecimalStr.length() == 4) {
            int bit1Num = Integer.parseInt(hexadecimalStr.substring(0, 1), 16);//获取第一位。判断是正数还是负数
            if (bit1Num < 8) { //小于8是正数
                getDataDecimal = Integer.parseInt(hexadecimalStr, 16);
            } else { //负数
                hexadecimalStr = "FFFF" + hexadecimalStr; //先不全八位
                getDataDecimal = new BigInteger(hexadecimalStr, 16).intValue();
            }
            return getDataDecimal;
        }
        return 0;
    }
 
    /**
     * 返回特定长度的16进制字符串
     *
     * @param data
     * @param length
     * @return
     */
    public static String getHexToLenght(String data, int length) {
        StringBuffer stringBuilder = new StringBuffer(data);
        for (int i = 0; i < length - data.length(); i++) {
            stringBuilder.insert(0, "0");
        }
        return stringBuilder.toString();
    }
 
    /**
     * 字节数组转16进制删除为0的
     *
     * @param bytes 需要转换的byte数组
     * @return 转换后的Hex字符串
     */
    public static String bytesToHexClean0(byte[] bytes) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xFF);
            if ("0".equals(hex))
                break;
            if (hex.length() < 2) {
                sb.append(0);
            }
            sb.append(hex);
        }
        return sb.toString();
    }
 
    /**
     * 解析域名获取IP数组
     *
     * @param host
     * @return
     */
    public static void parseHostGetIPAddress(String host, Handler handler) {
 
        new Thread(new Runnable() {
            @Override
            public void run() {
                String[] ipAddressArr = null;
                Message message = new Message();
                try {
                    String myhost = host.replace("http://", "");
                    myhost = myhost.replace("https://", "");
//                    InetAddress inetAddressArr = InetAddress.getByName(myhost);
                    InetAddress[] inetAddressArrs = InetAddress.getAllByName(myhost);
                    if (inetAddressArrs.length == 1) {
                        message.what = 1;
                        message.obj = inetAddressArrs[0].getHostAddress();
                    } else {
                        message.what = 2;
                        message.obj = inetAddressArrs;
 
                    }
                    handler.sendMessage(message);
 
 
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                    message.what = -1;
                    handler.sendMessage(message);
                }
 
            }
        }).start();
 
    }
 
    /**
     * 判断字符串是否只含数字和小数点
     *
     * @param str
     * @return
     */
    public static boolean isInteger(String str) {
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d&|.]*$");
        return pattern.matcher(str).matches();
    }
 
 
    public static String getOrderId(Context context) {
        String orderId = "000000000001";
        try {
            AdminDataBean villageNo = HNBaseDaoSingleton.getInstance(context).adminDao().findFirst();
//            String orderNo = WSHelper.getInstance(context).get(OrderNo, 1) + "";
            String orderNo = generateNumber8();
            if (villageNo != null) {
                String no = villageNo.getSerial();
                StringBuffer stringBuffer = new StringBuffer(no);
                for (int i = 0; i < 16 - no.length() - orderNo.length(); i++) {
                    stringBuffer.append("0");
                }
                stringBuffer.append(orderNo);
                return stringBuffer.toString();
            } else {
                StringBuffer stringBuffer = new StringBuffer();
                for (int i = 0; i < 16 - orderNo.length(); i++) {
                    stringBuffer.append("0");
                }
                stringBuffer.append(orderNo);
                return stringBuffer.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return orderId;
    }
 
    /**
     * 获取8为随机数
     *
     * @return
     */
    public static String generateNumber8() {
        String no = "";
        int num[] = new int[8];
        int c = 0;
        for (int i = 0; i < 8; i++) {
            num[i] = new Random().nextInt(10);
            c = num[i];
            for (int j = 0; j < i; j++) {
                if (num[j] == c) {
                    i--;
                    break;
                }
            }
        }
        if (num.length > 0) {
            for (int i = 0; i < num.length; i++) {
                no += num[i];
            }
        }
        return no;
    }
 
}