| 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; | 
|     } | 
|   | 
| } |