package toTcp; import java.util.Arrays; import static java.lang.Integer.parseInt; import static java.lang.Integer.toHexString; public class gethex { public static byte[] hexStrToBinaryStr(String hexString) { // if (TextUtils.isEmpty(hexString)) { // 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; } //BinaryToHexString(bytes); return bytes; } public static String BinaryToHexString(byte[] bytes) { String hexStr = "0123456789ABCDEF"; String result = ""; String hex = ""; for (byte b : bytes) { hex = String.valueOf(hexStr.charAt((b & 0xF0) >> 4)); hex += String.valueOf(hexStr.charAt(b & 0x0F)); result += hex + " "; } // System.out.println("------"); //System.out.println(result); return result; } public static double toSqlElectric(String DLT645) { String[] data=DLT645.split(" "); int start = 0; int end = data.length - 1; while (start < end) { String temp = data[start]; data[start] = data[end]; data[end] = temp; start++; end--; } int num1 = Integer.parseInt("33", 16); String result=""; for (int i = 2; i <6; i++) { int num2 = Integer.parseInt(data[i], 16); int num3 = num2 - num1 ; int num4=Integer.parseInt(Integer.toHexString(num3)); result+=String.format("%02d", num4); } return Math.round(Float.valueOf(result)) * 0.01d; } public static void main(String[] args) { String DLT645="68 23 01 00 91 03 57 68 91 08 33 33 34 33 37 8A 34 33 6D 16"; toSqlElectric(DLT645); } }