Using the IPAddress Java library it is simple, one line of code for each direction works for both IPv4 and IPv6. In fact, you can write code that works for both IPv4 and IPv6 as in the first example below.
Disclaimer: I am the project manager of that library.
IP-version agnostic using byte[] and/or BigInteger:
IPAddress loopback = new IPAddressString("::1").getAddress();
System.out.println(loopback.getValue());
IPAddress backAgain = new IPAddressGenerator().from(loopback.getBytes());
System.out.println(backAgain);
Use ints for IPv4:
IPv4Address loopbackv4 = new IPAddressString("127.0.0.1").getAddress().toIPv4();
System.out.println(loopbackv4.intValue());
IPv4Address backAgainv4 = new IPv4Address(loopbackv4.intValue());
System.out.println(backAgainv4);
Use BigInteger for IPv6:
IPv6Address loopbackv6 = new IPAddressString("::1").getAddress().toIPv6();
System.out.println(loopbackv6.getValue());
IPv6Address backAgainv6 = new IPv6Address(loopbackv6.getValue());
System.out.println(backAgainv6);
Output:
1
0:0:0:0:0:0:0:1
2130706433
127.0.0.1
1
0:0:0:0:0:0:0:1