Java IPv6 Address String for Bytes

How can I convert a String containing the destination package of the ipv6 engine package to a 16-byte array? I know about getBytes and encodings, but I cannot figure out which encoding I should use, or I need to convert this String to hexadecimal or not.

String ipv6 = "2001:0DB8:AC10:FE01:0000:0000:0000:0000"; byte[] bytes = ipv6.getBytes(); //must be a 16 byte array 

An example of what I want to do is just to illustrate. Aubert: I need to convert String to 16 byte array Thanks

+7
source share
2 answers

try it

  InetAddress a = InetAddress.getByName("2001:0DB8:AC10:FE01:0000:0000:0000:0000"); byte[] bytes = a.getAddress(); 
+17
source

The open source Java library IPAddress will handle a wide range of IPv6 addresses, so it can be used if your string needs to be verified or has a wide range of formats. Disclaimer: I am the project manager of this library.

Code example:

 String ipv6 = "::1"; try { IPAddressString str = new IPAddressString("::1"); IPAddress addr = str.toAddress(); byte[] bytes = addr.getBytes();' } catch(IPAddressStringException e) { //e.getMessage has validation error } 
+1
source

Source: https://habr.com/ru/post/1487796/


All Articles