Alex Rivera | Logout

Java IPv6 Address String to Bytes

Asked 2013-06-24T03:34:30.077
10

How can I convert a String containing the ipv6's machine packet destination to a 16 byte array? I know about getBytes and encodings, but I can't seem to understand which encoding I should use or if I have to convert that 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 wanna do, just to exemplify. Obs.: I have to convert the String to a 16 byte array Thanks

Edit
Report

1 Answer

22

try this

    InetAddress a = InetAddress.getByName("2001:0DB8:AC10:FE01:0000:0000:0000:0000");
    byte[] bytes = a.getAddress();
answered 2013-06-24T03:48:43.933

Your Answer