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

top.jfunc.common.utils.RadixUtil Maven / Gradle / Ivy

The newest version!
package top.jfunc.common.utils;

/**
 * 进制转换类
 * @author 熊诗言2017/11/28
 */
public class RadixUtil {
    private RadixUtil(){}

    /**
     * 字节数组转换为16进制字符串, 大写
     * @param b 字节数字
     * @return 十六进制字符串 小写
     */
    public static String toHex(byte[] b){
        return toHexLower(b).toUpperCase();
    }

    /**
     * 字节数组转换为16进制字符串, 小写
     * @param b 字节数字
     * @return 十六进制字符串 小写
     */
    public static String toHexLower(byte[] b){
        StringBuffer hexString = new StringBuffer();
        for(int i = 0; i < b.length; i++){
            String plainText = Integer.toHexString(0xff & b[i]);
            if(plainText.length() < 2){
                plainText = "0" + plainText;
            }
            hexString.append(plainText);
        }
        return hexString.toString();
    }

    /**
     * 16进制字符串转换为字节数组
     * @param hex 十六进制字符串
     * @return 字节数组
     */
    public static byte[] toBytes(String hex){
        byte[] digest = new byte[hex.length() / 2];
        for(int i = 0; i < digest.length; i++){
            String byteString = hex.substring(2 * i, 2 * i + 2);
            int byteValue = Integer.parseInt(byteString, 16);
            digest[i] = (byte)byteValue;
        }
        return digest;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy