This discussion is archived
8 Replies Latest reply: May 26, 2011 10:56 PM by EJP RSS

Convert IPv6 address to decimal

806677 Newbie
Currently Being Moderated
How do we convert an IPv6 address to its decimal equivalent using java?

I could find different tools on Internet but not the java code for the same.

Any help would be much appreciated.

Thanks.
  • 1. Re: Convert IPv6 address to decimal
    EJP Guru
    Currently Being Moderated
    The text representation of IPv6 addresses is hex, not decimal, e.g. ffff::1. java.net.Inet6Address has methods to do that.
  • 2. Re: Convert IPv6 address to decimal
    806677 Newbie
    Currently Being Moderated
    Hello EJP

    Please visit the following URL:

    https://www.ultratools.com/tools/decimalCalcResult?ipAddress=805b%3A2d9d%3Adc28%3A0000%3A0000%3Afc57%3Ad4c8%3A1fff

    This converts the IPv6 address (which is in hexadecimal format) to a decimal number.

    I need to implement this functionality. I could not find any method in java.net.Inet6Address class for this.

    To convert an IPv4 address which is in String format to its decimal equivalent, we can use following method:

    <code>
    long stringToLong(String ipAddress) {
              
              String[] octets = ipAddress.split("\\.");
              
              long intValue = 0;
              long currentOctet;
              
              for (int i = 0; i < octets.length; i++) {
                   currentOctet = new Integer(octets[(octets.length-1)-i]).intValue();
                   intValue = (currentOctet << i*8) | intValue;
              }
              
              return intValue;
         }
    </code>

    Edited by: 803674 on May 26, 2011 12:22 AM
  • 3. Re: Convert IPv6 address to decimal
    EJP Guru
    Currently Being Moderated
    I need to implement this functionality.
    Why? It's non-standard. It conceals the octet values. It can't be used directly with any Java API. It's useless.
  • 4. Re: Convert IPv6 address to decimal
    806677 Newbie
    Currently Being Moderated
    I need to categorize the hosts by network ranges. That is why I need to convert an IPv6 address to decimal and vice-versa.
  • 5. Re: Convert IPv6 address to decimal
    baftos Expert
    Currently Being Moderated
              InetAddress ia = InetAddress.getByName("2001:0:4137:9e76:34b7:2e31:3f57:fd9a");
              byte[] ba = ia.getAddress();
              System.out.println(Arrays.toString(ba));
    result:
    [32, 1, 0, 0, 65, 55, -98, 118, 52, -73, 46, 49, 63, 87, -3, -102]
    Now you have an array of 16 bytes and convert it the way you please.
    While an IPv4 address happens to fit in an int, the IPv6 does not fit in any primitive data type.
    I think no conversion of the byte array is needed at all to categorize by network ranges.
  • 6. Re: Convert IPv6 address to decimal
    EJP Guru
    Currently Being Moderated
    I need to categorize the hosts by network ranges. That is why I need to convert an IPv6 address to decimal and vice-versa.
    How exactly does converting it to decimal help you categorize hosts by network ranges, which are expressed in hexadecimal?
  • 7. Re: Convert IPv6 address to decimal
    806677 Newbie
    Currently Being Moderated
    I can give bottom and top IP addresses if I can convert an IPv6 address to decimal. And then when some IPv6 address is encountered to my application, I can convert it into decimal and then check whether it falls in the bottom and top range or not.

    However, I have now successfully converted an IPv6 address to decimal and vice-versa. The problem now is how to store such a large number (39 digits) in database (I am using derby) in numeric form.
  • 8. Re: Convert IPv6 address to decimal
    EJP Guru
    Currently Being Moderated
    I can give bottom and top IP addresses if I can convert an IPv6 address to decimal.
    You can do that in hex too. Or with BigInteger.
    And then when some IPv6 address is encountered to my application, I can convert it into decimal and then check whether it falls in the bottom and top range or not.
    And you can do that in hex too.
    However, I have now successfully converted an IPv6 address to decimal and vice-versa.
    You can do it with new BigInteger(0,Inet6Address.getAddress()).toString() in one direction, and InetAddress.getByAddress(new BigInteger(String).toByteArray()) in the other direction. But I don't see the point.
    The problem now is how to store such a large number (39 digits) in database (I am using derby) in numeric form.
    No it isn't. Store the 16 bytes in the database. If you must do this calculation in decimal, do the conversions on the fly. At least store the canonical representation in the database, keep your life simple.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points