java:十六进制转ip地址

 十六进制转ip地址

    private static String hexToIp(String hex){
       //hex= E2013311 结果226.1.51.17
        hex= hex.replaceAll("(.{2})", "$1.");
        if (hex.endsWith(".")) {
            hex= hex.substring(0, hex.length() - 1);
        }
        int ip01 = Integer.parseInt(hex.split("\\.")[0],16);
        int ip02 = Integer.parseInt(hex.split("\\.")[1],16);
        int ip03 = Integer.parseInt(hex.split("\\.")[2],16);
        int ip04 = Integer.parseInt(hex.split("\\.")[3],16);
        return ip01+"."+ip02+"."+ip03+"."+ip04;
    }

十六进制转字符

public static String hexStr2Str(String hexStr) {
    String str = "0123456789ABCDEF";
    char[] hexs = hexStr.toCharArray();
    byte[] bytes = new byte[hexStr.length() / 2];
    int n;
    for (int i = 0; i < bytes.length; i++) {
        n = str.indexOf(hexs[2 * i]) * 16;
        n += str.indexOf(hexs[2 * i + 1]);
        bytes[i] = (byte) (n & 0xff);
    }
    return new String(bytes);
}

你可能感兴趣的:(java,python,前端)