All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.feingto.iot.common.util.ParserKit Maven / Gradle / Ivy

There is a newer version: 2.3.3.RELEASE
Show newest version
package com.feingto.iot.common.util;

import io.netty.buffer.ByteBuf;

import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;

/**
 * 进制转换工具
 *
 * @author longfei
 */
public class ParserKit {
    /**
     * 将byteBuf转换成byte[]
     */
    public static byte[] copyByteBuf(ByteBuf byteBuf) {
        byte[] bytes = new byte[byteBuf.readableBytes()];
        byteBuf.readBytes(bytes);
        return bytes;
    }

    /**
     * 将byte[]转换成string
     */
    public static String byteToString(byte[] bytes) {
        StringBuilder stringBuffer = new StringBuilder();
        for (byte b : bytes) {
            stringBuffer.append((char) b);
        }
        return stringBuffer.toString();
    }

    /**
     * 十进制转十六进制,设置返回字符串长度(字节),前面补0
     */
    public static String intToHex(int len, int value) {
        String str = Integer.toHexString(value);
        String patch = toZeroString(len * 2, str, "0");
        return (patch + str).toUpperCase();
    }

    /**
     * 十六进制转十进制
     */
    public static int hexToInt(String str) {
        return Integer.parseInt(str, 16);
    }

    /**
     * string转十六进制,设置返回字符串长度(字节),前面补00
     */
    public static String stringToHex(int len, String s) {
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            int ch = (int) s.charAt(i);
            str.append(Integer.toHexString(ch));
        }
        String patch = toZeroString(len, s, "00");
        return (patch + str).toUpperCase();
    }

    /**
     * 十六进制转string
     */
    public static String hexToString(String s) {
        s = getReplaceStringHex(s, '0');
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(
                        s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        s = new String(baKeyword, StandardCharsets.UTF_8);
        return s;
    }

    /**
     * 字符串转十六进制,设置返回字符串长度(字节),前面位数不足的补0,适用于所有字符(包括中文)
     *
     * @param len 字节数
     * @param str 待编码的字符串
     */
    public static String encodeToHex(int len, String str) {
        str = encode(str);
        String patch = toZeroString(len * 2, str, "0");
        return (patch + str).toUpperCase();
    }

    /**
     * 十六进制转字符串,适用于所有字符(包括中文)
     */
    public static String decodeToString(String str) {
        return getDecodeString(str.getBytes(), 0, str.getBytes().length);
    }

    /**
     * 字节数组转字符串,设置起始位,字节数
     */
    public static String getDecodeString(byte[] arr, int offset, int len) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < len; i++) {
            sb.append((char) arr[offset + i]);
        }
        String strHex = getReplaceStringHex(sb.toString(), '0');
        return decode(strHex);
    }

    /**
     * 返回字符串,设置字符串长度(字节),前面补x
     */
    public static String toZeroString(int len, String s, String x) {
        StringBuilder patch = new StringBuilder();
        if (s.length() < len) {
            for (int i = 0; i < len - s.length(); i++) {
                patch.append(x);
            }
        }
        return patch.toString();
    }

    /**
     * 去除十六进制字符串里面的占位符号
     */
    public static String getReplaceStringHex(String stringHex, char regex) {
        String str;
        char[] arr = stringHex.toCharArray();
        int len = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != regex) {
                len = i;
                break;
            }
        }
        str = stringHex.substring(len);
        return str;
    }

    /**
     * 十六进制进制数字字符集
     */
    private static String hexString = "0123456789ABCDEF";

    /**
     * 将字符串编码成十六进制数字,适用于所有字符(包括中文)
     */
    public static String encode(String str) {
        // 根据默认编码获取字节数组
        byte[] bytes;
        StringBuilder sb;
        bytes = str.getBytes(StandardCharsets.UTF_8);
        sb = new StringBuilder(bytes.length * 2);
        // 将字节数组中每个字节拆解成2位16进制整数
        for (byte aByte : bytes) {
            sb.append(hexString.charAt((aByte & 0xf0) >> 4));
            sb.append(hexString.charAt((aByte & 0x0f)));
        }
        return sb.toString();
    }

    /**
     * 将十六进制数字解码成字符串,适用于所有字符(包括中文)
     */
    public static String decode(String bytes) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length() / 2);
        // 将每2位16进制整数组装成一个字节
        for (int i = 0; i < bytes.length(); i += 2) {
            baos.write((hexString.indexOf(bytes.charAt(i)) << 4 |
                    hexString.indexOf(bytes.charAt(i + 1))));
        }
        return new String(baos.toByteArray(), StandardCharsets.UTF_8);
    }

    public static void main(String[] args) {
        // 16进制转10进制
        System.out.println(ParserKit.hexToInt("76"));

        String str = ParserKit.intToHex(2, 151);
        System.out.println(str);
        System.out.println(ParserKit.hexToInt(str));

        str = ParserKit.stringToHex(100, "eeeeeeeeeeeeeeeeeeeeeeeeeee");
        System.out.println(str);
        System.out.println(ParserKit.hexToString(str));

        str = ParserKit.encodeToHex(20, "我想你");
        System.out.println(str);
        System.out.println(ParserKit.decodeToString(str));

        str = ParserKit.encode("我爱你");
        System.out.println(str);
        System.out.println(ParserKit.decodeToString(str));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy