java 将十六进制字符串转换为 IP 地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17489404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert hexadecimal string to IP Address
提问by Rg90
I want to convert a string value (in hexadecimal) to an IP Address. How can I do it using Java?
我想将字符串值(十六进制)转换为 IP 地址。我如何使用 Java 做到这一点?
Hex value: 0A064156
十六进制值: 0A064156
IP: 10.6.65.86
知识产权: 10.6.65.86
This sitegives me the correct result, but I am not sure how to implement this in my code.
这个站点给了我正确的结果,但我不确定如何在我的代码中实现它。
Can it be done directly in an XSLT?
可以直接在 XSLT 中完成吗?
回答by Evgeniy Dorofeev
try this
试试这个
InetAddress a = InetAddress.getByAddress(DatatypeConverter.parseHexBinary("0A064156"));
DatatypeConverter is from standard javax.xml.bind
package
DatatypeConverter 来自标准javax.xml.bind
包
回答by JREN
You can split your hex value in groups of 2 and then convert them to integers.
您可以将十六进制值分成 2 组,然后将它们转换为整数。
0A = 10
06 = 06
65 = 41
86 = 56
0A = 10
06 = 06
65 = 41
86 = 56
Code:
代码:
String hexValue = "0A064156";
String ip = "";
for(int i = 0; i < hexValue.length(); i = i + 2) {
ip = ip + Integer.valueOf(hexValue.subString(i, i+2), 16) + ".";
}
System.out.println("Ip = " + ip);
Output:
输出:
Ip = 10.6.65.86.
ip = 10.6.65.86。
回答by Devi Kiran
public String convertHexToString(String hex){
StringBuilder sb = new StringBuilder();
StringBuilder temp = new StringBuilder();
for( int i=0; i<hex.length()-1; i+=2 ){
String output = hex.substring(i, (i + 2));
int decimal = Integer.parseInt(output, 16);
sb.append((char)decimal);
temp.append(decimal);
temp.append(".");
}
System.out.println("Decimal : " + temp.toString());
return sb.toString();
}
}
回答by Rahul Bobhate
You can use the following method:
您可以使用以下方法:
public static String convertHexToIP(String hex)
{
String ip= "";
for (int j = 0; j < hex.length(); j+=2) {
String sub = hex.substring(j, j+2);
int num = Integer.parseInt(sub, 16);
ip += num+".";
}
ip = ip.substring(0, ip.length()-1);
return ip;
}
回答by Ryder
The accepted answer has a requirement that, the hex must be even-length. Here is my answer:
接受的答案有一个要求,即十六进制必须是偶数长度。这是我的回答:
private String getIpByHex(String hex) {
Long ipLong = Long.parseLong(hex, 16);
String ipString = String.format("%d.%d.%d.%d", ipLong >> 24,
ipLong >> 16 & 0x00000000000000FF,
ipLong >> 8 & 0x00000000000000FF,
ipLong & 0x00000000000000FF);
return ipString;
}
回答by The Tosters
You can split it 2 characters, and then use Integer.parse(string, radix) to convert to integer values
您可以将其拆分为 2 个字符,然后使用 Integer.parse(string, radix) 转换为整数值
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html#parseInt(java.lang.String,int)
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html#parseInt(java.lang.String,int)
UPDATE: Link for newer documentation: https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#parseInt-java.lang.String-int-
更新:更新文档的链接:https: //docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#parseInt-java.lang.String-int-