+ * 2017年9月21日
+ */
+public class ByteUtils {
+ /**
+ * 将int转为高端字节序排列的byte数组(Java内存存放顺序)
+ *
+ * @param n
+ * @return
+ */
+ public static byte[] int2ByteArray(int n) {
+ byte[] byteArray = null;
+ try {
+ ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
+ DataOutputStream dataOut = new DataOutputStream(byteOut);
+ dataOut.writeInt(n);
+ byteArray = byteOut.toByteArray();
+ Arrays.toString(byteArray);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ return byteArray;
+ }
+
+ /**
+ * 将int转为高字节在前,低字节在后的byte数组
+ *
+ * @param n int
+ * @return byte[]
+ */
+ public static byte[] int2Hbytes(int n) {
+ byte[] b = new byte[4];
+ b[3] = (byte) (n & 0xff);
+ b[2] = (byte) (n >> 8 & 0xff);
+ b[1] = (byte) (n >> 16 & 0xff);
+ b[0] = (byte) (n >> 24 & 0xff);
+
+ return b;
+ }
+
+ /**
+ * 将int转为高字节在前,低字节在后的byte数组,一字节
+ *
+ * @param n int
+ * @return byte[]
+ */
+ public static byte[] int2Hbytes1byte(int n) {
+ byte[] b = new byte[1];
+ b[0] = (byte) (n & 0xff);
+ return b;
+ }
+
+ /**
+ * 将short转为高字节在前,低字节在后的byte数组
+ *
+ * @param n short
+ * @return byte[]
+ */
+ public static byte[] short2Hbytes(short n) {
+ byte[] b = new byte[2];
+ b[1] = (byte) (n & 0xff);
+ b[0] = (byte) (n >> 8 & 0xff);
+ return b;
+ }
+
+ /**
+ * 以下 是整型数 和 网络字节序的 byte[] 数组之间的转换
+ *
+ * @param n
+ * @return
+ */
+ public static byte[] long2Hbytes(long n) {
+ byte[] b = new byte[8];
+ b[7] = (byte) (n & 0xff);
+ b[6] = (byte) (n >> 8 & 0xff);
+ b[5] = (byte) (n >> 16 & 0xff);
+ b[4] = (byte) (n >> 24 & 0xff);
+ b[3] = (byte) (n >> 32 & 0xff);
+ b[2] = (byte) (n >> 40 & 0xff);
+ b[1] = (byte) (n >> 48 & 0xff);
+ b[0] = (byte) (n >> 56 & 0xff);
+ return b;
+ }
+
+ public static byte[] long2H6bytes(long n) {
+ byte[] b = new byte[6];
+ b[5] = (byte) (n & 0xff);
+ b[4] = (byte) (n >> 8 & 0xff);
+ b[3] = (byte) (n >> 16 & 0xff);
+ b[2] = (byte) (n >> 24 & 0xff);
+ b[1] = (byte) (n >> 32 & 0xff);
+ b[0] = (byte) (n >> 40 & 0xff);
+ return b;
+ }
+
+ public static byte[] long2H4bytes(long n) {
+ byte[] b = new byte[4];
+ b[3] = (byte) (n & 0xff);
+ b[2] = (byte) (n >> 8 & 0xff);
+ b[1] = (byte) (n >> 16 & 0xff);
+ b[0] = (byte) (n >> 24 & 0xff);
+ return b;
+ }
+
+ public static byte[] unlong2H4bytes(long n) {
+ byte[] b = new byte[4];
+ b[0] = (byte) (n & 0xff);
+ b[1] = (byte) (n >> 8 & 0xff);
+ b[2] = (byte) (n >> 16 & 0xff);
+ b[3] = (byte) (n >> 24 & 0xff);
+ return b;
+ }
+
+
+ /**
+ * 合并数组
+ *
+ * @param first
+ * @param rest
+ * @return
+ */
+ public static byte[] concatBytes(byte[] first, byte[]... rest) {
+ int totalLength = first.length;
+ for (byte[] array : rest) {
+ if (null != array) {
+ totalLength += array.length;
+ }
+ }
+ byte[] result = Arrays.copyOf(first, totalLength);
+ int offset = first.length;
+ for (byte[] array : rest) {
+ if (null != array) {
+ System.arraycopy(array, 0, result, offset, array.length);
+ offset += array.length;
+ }
+ }
+ return result;
+ }
+
+ /**
+ * byte数组转为十六进制字符串
+ *
+ * @param bytes
+ * @return
+ */
+ public static String byte2Hex(byte[] bytes) {
+ StringBuffer hexString = new StringBuffer();
+ for (int i = 0; i < bytes.length; i++) {
+ String hex = Integer.toHexString(0xff & bytes[i]);
+
+ if (hex.length() == 1) {
+ hexString.append('0');
+ }
+ hexString.append(hex);
+ }
+ return hexString.toString();
+ }
+
+ /**
+ * Convert hex string to byte[]
+ *
+ * @param hexString the hex string
+ * @return byte[]
+ */
+ public static byte[] hex2Byte(String hexString) {
+ if (hexString == null || hexString.equals("")) {
+ return null;
+ }
+ hexString = hexString.toUpperCase().replace(" ", "");
+ int length = hexString.length() / 2;
+ char[] hexChars = hexString.toCharArray();
+ byte[] d = new byte[length];
+ for (int i = 0; i < length; i++) {
+ int pos = i * 2;
+ d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
+ }
+ return d;
+ }
+
+ /**
+ * Convert char to byte
+ *
+ * @param c char
+ * @return byte
+ */
+ private static byte charToByte(char c) {
+ return (byte) "0123456789ABCDEF".indexOf(c);
+ }
+
+ /**
+ * 输入流转为字节数组
+ */
+ public static byte[] toByteArray(InputStream input) throws IOException {
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
+ byte[] buffer = new byte[4096];
+ int n = 0;
+ while (-1 != (n = input.read(buffer))) {
+ output.write(buffer, 0, n);
+ }
+ return output.toByteArray();
+ }
+
+
+ /**
+ * bytes字符串转换为Byte值
+ *
+ * @param src src Byte字符串,每个Byte之间没有分隔符
+ * @return byte[]
+ */
+ public static byte[] hexStr2Bytes(String src) {
+ int m = 0, n = 0;
+ int l = src.length() / 2;
+ byte[] ret = new byte[l];
+ for (int i = 0; i < l; i++) {
+ m = i * 2 + 1;
+ n = m + 1;
+ ret[i] = Byte.decode("0x" + src.substring(i * 2, m) + src.substring(m, n));
+ }
+ return ret;
+ }
+
+// /**
+// * 字节数组转换成16进制字符串
+// * @param bytes 字节数组
+// * @return 16进制字符串
+// */
+// public static String hexEncode(byte[] bytes) {
+// if (bytes == null || bytes.length <= 0) {
+// return null;
+// }
+// return new String(Hex.encodeHex(bytes)); //Hex.encodeHex(bytes, false)
+// }
+//
+// /**
+// * 16进制字符串转换成字节数组
+// * @param hexStr 16进制字符串
+// * @return 字节数组
+// */
+// public static byte[] hexDecode(String hexStr) {
+// if (hexStr == null || "".equals(hexStr)) {
+// return null;
+// }
+// try {
+// char[] cs = hexStr.toCharArray();
+// return Hex.decodeHex(cs);
+// } catch (DecoderException e) {
+// e.printStackTrace();
+// }
+// return null;
+// }
+
+
+ /**
+ * 字符串转换成十六进制字符串
+ *
+ * @param s s为待转换的ASCII字符串
+ */
+ public static String str2HexStr(String s) {
+
+ String str = "";
+ for (int i = 0; i < s.length(); i++) {
+ int ch = (int) s.charAt(i);
+ String s4 = Integer.toHexString(ch);
+ str = str + s4;
+ }
+ return str;
+ }
+
+ public static String int2HexStr(Integer n) {
+
+ String str = Integer.toHexString(n);
+ while (str.length() < 18) {
+ str = "0" + str;
+ }
+ return str.toUpperCase();
+ }
+
+ public static String binaryToHex(String bin) {
+ String str = Long.toHexString(Long.parseLong(bin, 2));
+ while (str.length() < 4) {
+ str = "0" + str;
+ }
+ return str.toUpperCase();
+ }
+
+ /**
+ * 16进制的字符串转byte数组
+ *
+ * @param src
+ * @return
+ */
+ public static byte[] str16ToBytes(String src) {
+ int w = 0;
+ byte[] bytes_2 = new byte[src.length() / 2];
+ for (int i = 0; i < src.length(); i++) {
+ String zz = src.substring(i, i + 2);
+ byte aaa = (byte) Integer.parseInt(zz, 16);
+ bytes_2[w] = aaa;
+ i++;
+
+ w = w + 1;
+ }
+
+ return bytes_2;
+ }
+
+ /**
+ * 16进制转10进制数字
+ *
+ * @param src
+ * @return
+ */
+ public static int str16ToInt10(String src) {
+ return Integer.parseInt(src, 16);
+ }
+
+ /**
+ * 16进制转10进制数字
+ *
+ * @param src
+ * @return
+ */
+ public static long str16ToLong10(String src) {
+ return Long.parseLong(src, 16);
+ }
+
+ public static String convertStringToHex(String str) {
+ char[] chars = str.toCharArray();
+ StringBuffer hex = new StringBuffer();
+ for (int i = 0; i < chars.length; i++) {
+ hex.append(Integer.toHexString((int) chars[i]));
+ }
+ return hex.toString();
+ }
+
+ public static String convertHexToString(String hex) {
+ hex = hex.replace(" ", "");
+ StringBuilder sb = new StringBuilder();
+ StringBuilder temp = new StringBuilder();
+
+ //49204c6f7665204a617661 split into two characters 49, 20, 4c...
+ for (int i = 0; i < hex.length() - 1; i += 2) {
+
+ //grab the hex in pairs
+ String output = hex.substring(i, (i + 2));
+ //convert hex to decimal
+ int decimal = Integer.parseInt(output, 16);
+ //convert the decimal to character
+ sb.append((char) decimal);
+
+ temp.append(decimal);
+ }
+
+ return sb.toString();
+ }
+
+ /**
+ * @param
+ * @ClassName
+ * @Description : 功能说明
+ * @Return :
+ * @Author : li
+ * @Date : 2021/3/25 13:48
+ */
+ public static byte[] hexStringToByteArray(String hexString) {
+ hexString = hexString.replaceAll(" ", "");
+ int len = hexString.length();
+ byte[] bytes = new byte[len / 2];
+ for (int i = 0; i < len; i += 2) {
+ // 两位一组,表示一个字节,把这样表示的16进制字符串,还原成一个字节
+ bytes[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character
+ .digit(hexString.charAt(i + 1), 16));
+ }
+ return bytes;
+ }
+
+ /**
+ * @param
+ * @ClassName
+ * @Description : 功能说明
+ * @Return :
+ * @Author : li
+ * @Date : 2021/3/25 13:48
+ */
+ public static String bytesToHexString(byte[] bytes) {
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < bytes.length; i++) {
+ String hex = Integer.toHexString(0xFF & bytes[i]);
+ if (hex.length() == 1) {
+ sb.append('0');
+ }
+ sb.append(hex);
+ }
+ return sb.toString();
+ }
+
+ /**
+ * 从16进制字符串中获取2进制为1的序列号
+ *
+ * @param src
+ * @return
+ */
+ public static ArrayList Str16Get1(String src) {
+ byte[] b_p = ByteUtils.str16ToBytes(src);
+ ArrayList list = new ArrayList<>();
+ int w = 0;
+ for (int i = 0; i < src.length() / 2; i++) {
+ for (int j = 7; j >= 0; j--) {
+ if ((b_p[i] & (1 << j)) != 0) {
+ int pp = i * 8 + 8 - j;
+ System.out.println("pppp=======" + pp);
+ list.add(w, pp);
+ w = w + 1;
+ } else {
+
+ }
+ }
+
+ }
+ return list;
+ }
+
+ /**
+ * 异或校验
+ *
+ * @param bytes
+ * @return
+ */
+ public static byte[] bytesXorCrc(byte[] bytes) {
+ byte[] crc = new byte[1];// 异或校验
+ crc[0] = bytes[0];
+ for (int i = 1; i < bytes.length; i++) {
+ crc[0] ^= bytes[i];
+ }
+ return crc;
+ }
+
+
+ /**
+ * 和校验
+ *
+ * @param data
+ * @return
+ */
+ public static String makeChecksum(String data) {
+ data = data.replace(" ", "");
+ if (data == null || data.equals("")) {
+ return "";
+ }
+ int total = 0;
+ int len = data.length();
+ int num = 0;
+ while (num < len) {
+ String s = data.substring(num, num + 2);
+ total += Integer.parseInt(s, 16);
+ num = num + 2;
+ }
+ /**
+ * 用256求余最大是255,即16进制的FF
+ */
+ int mod = total % 256;
+ String hex = Integer.toHexString(mod);
+ len = hex.length();
+ // 如果不够校验位的长度,补0,这里用的是两位校验
+ if (len < 2) {
+ hex = "0" + hex;
+ }
+ hex = hex.toUpperCase();
+ if (hex.equals("7E")) {
+ hex = "7F 01";
+ } else if (hex.equals("7F")) {
+ hex = "7F 02";
+ }
+ return hex;
+ }
+}
diff --git a/lib-serialport/src/main/res/values/strings.xml b/lib-serialport/src/main/res/values/strings.xml
new file mode 100644
index 0000000..986c664
--- /dev/null
+++ b/lib-serialport/src/main/res/values/strings.xml
@@ -0,0 +1,3 @@
+