gpt4 book ai didi

Java:将 int 转换为 InetAddress

转载 作者:IT老高 更新时间:2023-10-28 21:12:11 33 4
gpt4 key购买 nike

我有一个 int,其中包含一个按网络字节顺序排列的 IP 地址,我想将其转换为 InetAddress 对象。我看到有一个 InetAddress 构造函数采用 byte[],是否有必要将 int 转换为 byte[ ]先,还是有别的办法?

最佳答案

测试和工作:

int ip  = ... ;
String ipStr =
String.format("%d.%d.%d.%d",
(ip & 0xff),
(ip >> 8 & 0xff),
(ip >> 16 & 0xff),
(ip >> 24 & 0xff));

关于Java:将 int 转换为 InetAddress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1957637/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com