Convert hexadecimal string to IP Address

后端 未结 6 1594
囚心锁ツ
囚心锁ツ 2021-01-02 03:11

I want to convert a string value (in hexadecimal) to an IP Address. How can I do it using Java?

Hex value: 0A064156

IP: 10.6.65.86<

6条回答
  •  不知归路
    2021-01-02 03:57

    You can use the following method:

    public static String convertHexToIP(String hex)
    {
        String ip= "";
    
        for (int j = 0; j < hex.length(); j+=2) {
            String sub = hex.substring(j, j+2);
            int num = Integer.parseInt(sub, 16);
            ip += num+".";
        }
    
        ip = ip.substring(0, ip.length()-1);
        return ip;
    }
    

提交回复
热议问题