Convert hexadecimal string to IP Address

后端 未结 6 1562
囚心锁ツ
囚心锁ツ 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:38

    You can split your hex value in groups of 2 and then convert them to integers.

    0A = 10

    06 = 06

    65 = 41

    86 = 56

    Code:

    String hexValue = "0A064156";
    String ip = "";
    
    for(int i = 0; i < hexValue.length(); i = i + 2) {
        ip = ip + Integer.valueOf(hexValue.subString(i, i+2), 16) + ".";
    }
    
    System.out.println("Ip = " + ip);
    

    Output:

    Ip = 10.6.65.86.

提交回复
热议问题