Java开发中常用的工具类

一、16进制与10进制转换

public class HexConverterUtil{
    public static int convert2Int(String hexString){
        Converter converter;
        if (hexString.startsWith("0x")){
            converter = from -> Integer.parseInt(from.substring(2), 16);
            return converter.convert(hexString);
        }else {
            converter = from -> Integer.parseInt(from, 16);
            return converter.convert(hexString);
        }
    }

    public static String convert2Hex(Integer intValue){
        Converter converter;
        converter = from -> Integer.toHexString(from);
        String value = converter.convert(intValue);
        int len = value.length();
        if (value.length() < 8){
            for (int i = 0; i < 8 - len; i++) {
                value = "0" + value;
            }
        }
        return value;
    }
}
@FunctionalInterface
interface Converter {
    T convert(F from);
}

你可能感兴趣的:(Java开发中常用的工具类)