Java中有没有对应C++中的inet_toa()的方法? 后台是一个C++程序,进行了一个ip地址的转换,前台需要用Java进行转换,哪位指点一下? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 inet_toa();是将一个ipv4的地址转化为字符串,java中对应的有:InetAddress getByAddress(byte[] addr) ; 是这样,数据库中存在一个数值型字段,存放把类似一个数值,需要通过java把该数值转换为实际的IP地址。数据库中的数值,是通过C++ inet_ntoa()转换的。同样,我也需要通过java将一个正常的ip地址转换为一个数值,共后台的c++程序反向解析 由于java中没有无符号数, 所以用int操作起来很麻烦, 不如用4个byte来操作还有就是lz用java和c++和用的时候, 要注意字节的顺序public static byte[] inet_addr(String ipstr) { try { InetAddress ia = InetAddress.getByName(ipstr); byte[] iparr = ia.getAddress(); return iparr; } catch (UnknownHostException uhe) { uhe.printStackTrace(); return null; } } public static String inet_toa(byte[] iparr) { try { InetAddress ia = InetAddress.getByAddress(iparr); String ipstr = ia.getHostAddress(); return ipstr; } catch (UnknownHostException uhe) { uhe.printStackTrace(); return null; } } 汇集大家的思路,弄了个全的,请大家参考:import java.net.InetAddress;import java.net.UnknownHostException;/** * 192.168.0.18 -1062731758 */public class TestIPNum { public static void main(String[] args) throws UnknownHostException { String host = TestIPNum.inet_toa(-1062731758); System.out.println(host); System.out.println(TestIPNum .inet_addr("192.168.0.18")); System.out.println(Integer.MAX_VALUE); System.out.println(Integer.MIN_VALUE); } public static int inet_addr(String ipstr) { try { InetAddress ia = InetAddress.getByName(ipstr); byte[] iparr = ia.getAddress(); int iRet = byte2Int(iparr); return iRet; } catch (UnknownHostException uhe) { uhe.printStackTrace(); return 0; } } public static String inet_toa(int ipInt) { try { byte[] iparr = int2Byte(ipInt); InetAddress ia = InetAddress.getByAddress(iparr); String ipstr = ia.getHostAddress(); return ipstr; } catch (UnknownHostException uhe) { uhe.printStackTrace(); return null; } } public static byte[] int2Byte(int intValue) { byte[] b = new byte[4]; for (int i = 0; i < 4; i++) { b[i] = (byte) (intValue >> 8 * (3 - i) & 0xFF); } return b; } public static int byte2Int(byte[] b) { int intValue = 0; for (int i = 0; i < b.length; i++) { intValue += (b[i] & 0xFF) << (8 * (3 - i)); } return intValue; }} IoBuffer是怎样的一种数据结构 我对现在CSDN的论坛设计很不满意! 关于javaObject类的一个小问题 求救,急jsp+flash实现.swf文件的播放 怎样给JLabel添加颜色?我菜啊~~ 读取文件的内容怎样保存在一个数组里 请问大侠java的类库都在哪儿呢? 请大家帮我看看这段代码问题出在哪?谢谢了! java中“|”和“&”的用法,请教大家,最好给个合理的解释! 成千上万的字符串,大多数只用一次,如何回收? 关于 equals方法 一个小疑问! 小问题,憋死我了,救我啊!!
java中对应的有:InetAddress getByAddress(byte[] addr) ;
数据库中的数值,是通过C++ inet_ntoa()转换的。同样,我也需要通过java将一个正常的ip地址转换为一个数值,共后台的c++程序反向解析
public static byte[] inet_addr(String ipstr) {
try {
InetAddress ia = InetAddress.getByName(ipstr);
byte[] iparr = ia.getAddress();
return iparr;
}
catch (UnknownHostException uhe) {
uhe.printStackTrace();
return null;
}
}
public static String inet_toa(byte[] iparr) {
try {
InetAddress ia = InetAddress.getByAddress(iparr);
String ipstr = ia.getHostAddress();
return ipstr;
}
catch (UnknownHostException uhe) {
uhe.printStackTrace();
return null;
}
}
import java.net.InetAddress;
import java.net.UnknownHostException;/**
* 192.168.0.18 -1062731758
*/
public class TestIPNum { public static void main(String[] args) throws UnknownHostException { String host = TestIPNum.inet_toa(-1062731758);
System.out.println(host); System.out.println(TestIPNum
.inet_addr("192.168.0.18"));
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
} public static int inet_addr(String ipstr) {
try {
InetAddress ia = InetAddress.getByName(ipstr);
byte[] iparr = ia.getAddress(); int iRet = byte2Int(iparr);
return iRet;
} catch (UnknownHostException uhe) {
uhe.printStackTrace();
return 0;
}
} public static String inet_toa(int ipInt) {
try {
byte[] iparr = int2Byte(ipInt);
InetAddress ia = InetAddress.getByAddress(iparr);
String ipstr = ia.getHostAddress(); return ipstr;
} catch (UnknownHostException uhe) {
uhe.printStackTrace();
return null;
}
} public static byte[] int2Byte(int intValue) {
byte[] b = new byte[4];
for (int i = 0; i < 4; i++) {
b[i] = (byte) (intValue >> 8 * (3 - i) & 0xFF);
}
return b;
} public static int byte2Int(byte[] b) {
int intValue = 0;
for (int i = 0; i < b.length; i++) {
intValue += (b[i] & 0xFF) << (8 * (3 - i));
}
return intValue;
}}