package com.hboxs.serialport.util;

public class StringUtils {
    /**
     * 反转字符串处理高位在前，低位在后
     * @param dataStr
     * @return
     */
    public static String revertDataStr(String dataStr) {
        // 1. 给字符串每两位加一个空格（每两位为一个字节）
        String regex = "(.{2})";
        dataStr = dataStr.replaceAll(regex, "$1 ");
        String[] dataStrArr = dataStr.split(" ");
        // 2. 反转数组
        int i = 0, j = dataStrArr.length - 1;
        String tmp;
        while (j > i) {
            tmp = dataStrArr[j];
            dataStrArr[j] = dataStrArr[i];
            dataStrArr[i] = tmp;
            j--;
            i++;
        }
        // 3. 拼接回字符串
        StringBuilder stringBuilder = new StringBuilder();
        for (String aDataStrArr : dataStrArr) {
            stringBuilder.append(aDataStrArr);
        }
        return stringBuilder.toString();
    }
}
