Java IPv6 address string to byte

How to convert a string containing IPv6 machine packet targets into a 16 byte array? I know GetBytes and encoding, but I don't seem to understand which encoding I should use, or whether I have to convert the string to hexadecimal

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

I want to do an example, just for illustration Obs.: I have to convert string to 16 byte array. Thank you

Solution

Try this

InetAddress a = InetAddress.getByName("2001:0DB8:AC10:FE01:0000:0000:0000:0000");
    byte[] bytes = a.getAddress();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>