进制转换工具类

本文介绍了ParseUtil类中实现的进制转换功能,包括16进制字符串转10进制、16进制转2进制字符串,以及对应的10进制和2进制之间的转换。通过实例方法演示了如何在Java中高效进行这些基础的数字格式转换。

16 10 2 进制转换

/**
 * @description: 进制转换
 **/
public class ParseUtil {
 
    /**
    * @description: 16进制字符串转10进制
    */
    public static Integer hexStringToDecimal(String hex) {
        Integer result = Integer.valueOf(hex, 16);
        return result;
    }
 
    /**
     * @description: 16进制字符串转2进制字符串
     */
    public static String hexStringToBinaryString(String hex) {
        Integer temp = Integer.valueOf(hex, 16);
        String result = Integer.toBinaryString(temp);
        return result;
    }
 
    /** 
    * @description: 10进制转16进制字符串 
    */ 
    public static String decimalToHexString(Integer decimal) {
        String result = Integer.toHexString(decimal);
        return result;
    }
 
    /**
     * @description: 10进制转2进制字符串
     */
    public static String decimalToBinaryString(Integer decimal) {
        String result = Integer.toBinaryString(decimal);
        return result;
    }
 
    /**
     * @description: 2进制字符串转10进制
     */
    public static Integer binaryStringToDecimal(String binary) {
        Integer result = Integer.valueOf(binary, 2);
        return result;
    }
 
    /**
     * @description: 2进制字符串转16进制字符串
     */
    public static String binaryStringToHexString(String binary) {
        Integer temp = Integer.valueOf(binary, 2);
        String result = Integer.toHexString(temp);
        return result;
    }
 
    /**
     * @description: byte[]转字符串
     */
    public static String byte2Hex(byte[] bytes) {
        StringBuffer stringBuffer = new StringBuffer();
        String temp = null;
        for (int i = 0; i < bytes.length; i++) {
            temp = Integer.toHexString(bytes[i] & 0xff);
            if (temp.length() == 1) {
                // 得到的一位的进行补0操作
                stringBuffer.append("0");
            }
            stringBuffer.append(temp);
        }
        return stringBuffer.toString();
    }
 
    /**
     * @description: byte[]转String[]
      */
    public static String[] bytesToStrings(byte[] bytes) {
        String[] strings = new String[bytes.length];
        for (int i = 0; i < bytes.length; i++) {
            String temp = Integer.toHexString(bytes[i] & 0xff);
            if (temp.length() == 1) {
                strings[i] = '0' + temp;
            } else {
                strings[i] = temp;
            }
        }
        return strings;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值