package tbDataModel_Dell; import JNADell.DellJAN; import Method.HexConvert; import tbDataModel_Dell.ModbusUtil; @SuppressWarnings("unused") public class Tools { /** * ½«Ê®Áù½øÖƵÄ×Ö·û´®×ª»»³É×Ö½ÚÊý×é * * @param hexString * @return */ public static byte[] hexStrToBinaryStr(String hexString) { if (hexString==null) { return null; } hexString = hexString.replaceAll(" ", ""); int len = hexString.length(); int index = 0; byte[] bytes = new byte[len / 2]; while (index < len) { String sub = hexString.substring(index, index + 2); bytes[index/2] = (byte)Integer.parseInt(sub,16); index += 2; } return bytes; } /** * ½«Ê®Áù½øÖƵÄ×Ö·û´®×ª»»³É×Ö½ÚÊý×鲻ȥ³ý¿Õ¸ñ * * @param hexString * @return */ public static byte[] hexStrToBinaryStr2(String hexString) { if (hexString==null) { return null; } int len = hexString.length(); int index = 0; byte[] bytes = new byte[len / 2]; while (index < len) { String sub = hexString.substring(index, index + 2); bytes[index/2] = (byte)Integer.parseInt(sub,16); index += 2; } return bytes; } public static final int registersToInt(byte[] bytes) { return ( ((bytes[0] & 0xff) << 24) | ((bytes[1] & 0xff) << 16) | ((bytes[2] & 0xff) << 8) | (bytes[3] & 0xff) ); }//registersToInt /**½«×Ö½ÚÊý×éתΪ16½øÖÆ×Ö·û´®*/ public static String Bytes2HexString(byte[] b) { String ret = ""; for (int i = 0; i < b.length; i++) { String hex = Integer.toHexString(b[i] & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } ret += hex.toUpperCase(); } return ret; } //assiccת16½øÖÆ public static byte[] assictohex(byte[] bytes){ byte by[]=new byte[4]; by[0]=0; by[1]=0; by[2]=(byte) Integer.parseInt(Tools.convertHexToString(Tools.Bytes2HexString(bytes)).substring(0, 2), 16); by[3]=(byte) Integer.parseInt(Tools.convertHexToString(Tools.Bytes2HexString(bytes)).substring(2, 4), 16); return by; } public static String convertHexToString(String hex){ StringBuilder sb = new StringBuilder(); StringBuilder temp = new StringBuilder(); for( int i=0; i