internship:普通常用的工具类编写

以下是工具类里的一些编码转换方法



import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.io.*;
import java.net.Socket;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;


@Slf4j
public class CommonUtil {

    /**
     * Ascii转换为字符串
     *
     * @param value
     * @return
     */
    public static String asciiToString(String value) {
        try {
            if (value == null || "".equalsIgnoreCase(value)) {
                return null;
            }
            StringBuffer sbu = new StringBuffer();
            String[] chars = value.split(",");
            for (String aChar : chars) {
                sbu.append((char) Integer.parseInt(aChar.trim()));
            }
            return sbu.toString();
        } catch (Exception e) {
            log.info("resolve data: " + value + " error!" + e + e.getMessage());
        }
        return null;
    }

    /**
     * 16进制转为10进制转为字符
     *
     * @param str
     * @return example: $PTNL, PJK, 022330.00, 052018,+3818626.239,N,+504060.470,E,3,20,1.3,EHT+67.967,M*75
     */
    public static String hexToStr(String str) {
        if (str.isEmpty() || str.length() < 5) {
            return null;
        }
        try {
            str = str.replaceAll("[\r\n]*", "");
            List<String> list = new ArrayList<>();
            String[] strArr = str.split("");
            for (int i = 0; i < strArr.length - 1; i += 2) {
                //拆分并转成10进制
                list.add(Integer.parseInt(strArr[i] + strArr[i + 1], 16) + "");
            }
            String tempStr = list.toString();
            String tempStr2 = tempStr.substring(1, tempStr.length() - 1);
            String result = asciiToString(tempStr2).replaceAll("[\r\n]*", "");
            return result;
        } catch (Exception e) {
            log.info("resolve data error!" + e + e.getMessage());
        }
        return null;
    }

    /**
     * 中文转unicode编码
     */
    public static String convertUnicode(final String string) {
        if (StringUtils.isEmpty(string)) {
            return null;
        }
        StringBuffer unicode = new StringBuffer();
        for (int i = 0; i < string.length(); i++) {
            char c = string.charAt(i);
            unicode.append("\\u" + Integer.toHexString(c));
        }
        return unicode.toString();
    }

    /**
     * unicode编码转中文
     */
    public static String decodeUnicode(final String dataStr) {
        String[] strArr = dataStr.split("\\\\u");
        String returnStr = "";
        for (int i = 1; i < strArr.length; i++) {
            returnStr += (char) Integer.valueOf(strArr[i], 16).intValue();
        }
        return returnStr;
    }

    /**
     * 关闭资源/连接,注意关闭顺序
     *
     * @param objects
     */
    public static boolean close(Object... objects) throws Exception {
        if (objects == null || objects.length < 1) {
            return false;
        }
        for (Object object : objects) {
            if (object == null) {
                continue;
            }
            if (object instanceof Connection) {
                ((Connection) object).close();
            }
            if (object instanceof PreparedStatement) {
                ((PreparedStatement) object).close();
            }
            if (object instanceof ResultSet) {
                ((ResultSet) object).close();
            }
            if (object instanceof Socket) {
                ((Socket) object).close();
            }
            if (object instanceof FileReader) {
                ((FileReader) object).close();
            }
            if (object instanceof FileWriter) {
                ((FileWriter) object).close();
            }
            if (object instanceof FileInputStream) {
                ((FileInputStream) object).close();
            }
            if (object instanceof FileOutputStream) {
                ((FileOutputStream) object).close();
            }
            if (object instanceof BufferedReader) {
                ((BufferedReader) object).close();
            }
            if (object instanceof BufferedWriter) {
                ((BufferedWriter) object).close();
            }
            if (object instanceof BufferedInputStream) {
                ((BufferedInputStream) object).close();
            }
            if (object instanceof BufferedOutputStream) {
                ((BufferedOutputStream) object).close();
            }
        }
        return false;
    }

    /**
     * 计算字符串的crc校验码
     *
     * @param data
     * @return
     */
    public static String getStrCrc(String data) {
        data = data.replace(" ", "");
        int len = data.length();
        if (len % 2 != 0) {
            return "0000";
        }
        int num = len / 2;
        byte[] para = new byte[num];
        for (int i = 0; i < num; i++) {
            int value = Integer.valueOf(data.substring(i * 2, 2 * (i + 1)), 16);
            para[i] = (byte) value;
        }
        return getCrc(para);
    }

    /**
     * 计算字节数组的crc校验码
     *
     * @param bytes
     * @return
     */
    public static String getCrc(byte[] bytes) {
        //CRC寄存器全为1
        int CRC = 0x0000ffff;
        //多项式校验值
        int POLYNOMIAL = 0x0000a001;
        int i, j;
        for (i = 0; i < bytes.length; i++) {
            CRC ^= ((int) bytes[i] & 0x000000ff);
            for (j = 0; j < 8; j++) {
                if ((CRC & 0x00000001) != 0) {
                    CRC >>= 1;
                    CRC ^= POLYNOMIAL;
                } else {
                    CRC >>= 1;
                }
            }
        }
        //结果转换为16进制
        String result = Integer.toHexString(CRC).toUpperCase();
        if (result.length() != 4) {
            StringBuffer sb = new StringBuffer("0000");
            result = sb.replace(4 - result.length(), 4, result).toString();
        }
        //交换高低位
        return result;
    }


    /**
     * 16进制字符串转为byte数组
     *
     * @param hexStr
     * @return
     */
    public static byte[] hexStrToByteArray(String hexStr) {
        byte[] arr = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() - 1; i += 2) {
            int a = Integer.valueOf(hexStr.substring(i, i + 2), 16);
            arr[i / 2] = (byte) a;
        }
        return arr;
    }

    public static byte[] int2Bytes(int integer) {
        byte[] bytes = new byte[4];
        bytes[3] = (byte) (integer >> 24);
        bytes[2] = (byte) (integer >> 16);
        bytes[1] = (byte) (integer >> 8);
        bytes[0] = (byte) integer;
        return bytes;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值