How to convert a Hex to IP address in java?
I have a problem converting hex to IP address.
For example, I have two string "b019e85" an "ac12accf" which represent IP of "11.1.158.133" and "172.18.172.207". Is there a convenient way of converting such hex string to ip address?
I have read a number of answers, such as onvert-hexadecimal-string-to-ip-address and java-convert-int-to-inetaddress.
But both of them do not work. Either it throws exception of "java.net.UnknownHostException: addr is of illegal length" or "java.lang.IllegalArgumentException: hexBinary needs to be even-length: b019e85
"
Sample code as follows:
InetAddress vipAddress = InetAddress.getByAddress(DatatypeConverter.parseHexBinary("b019e85"));
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I write a small utility method to convert hex to ip(only ipv4). It does not do the validity check.
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;
}
java ip hex
add a comment |
I have a problem converting hex to IP address.
For example, I have two string "b019e85" an "ac12accf" which represent IP of "11.1.158.133" and "172.18.172.207". Is there a convenient way of converting such hex string to ip address?
I have read a number of answers, such as onvert-hexadecimal-string-to-ip-address and java-convert-int-to-inetaddress.
But both of them do not work. Either it throws exception of "java.net.UnknownHostException: addr is of illegal length" or "java.lang.IllegalArgumentException: hexBinary needs to be even-length: b019e85
"
Sample code as follows:
InetAddress vipAddress = InetAddress.getByAddress(DatatypeConverter.parseHexBinary("b019e85"));
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I write a small utility method to convert hex to ip(only ipv4). It does not do the validity check.
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;
}
java ip hex
the problem is in arguments: b019e85 is 7 digits. Needs to be 8. Even error says it...
– Sarief
Nov 21 '18 at 7:14
Make b019e85 into 0b019e85, then parse the parts of the string.. You can parse this with Integer.parseInt(hex,16),where hex is substring of the hex string, and construct the full IP address with StringBuilder afterwards. Out of my head, this is how I would do it
– Atizs
Nov 21 '18 at 7:15
posted an answer based on suggestion in comment from user43648
– daljian
Nov 21 '18 at 8:01
add a comment |
I have a problem converting hex to IP address.
For example, I have two string "b019e85" an "ac12accf" which represent IP of "11.1.158.133" and "172.18.172.207". Is there a convenient way of converting such hex string to ip address?
I have read a number of answers, such as onvert-hexadecimal-string-to-ip-address and java-convert-int-to-inetaddress.
But both of them do not work. Either it throws exception of "java.net.UnknownHostException: addr is of illegal length" or "java.lang.IllegalArgumentException: hexBinary needs to be even-length: b019e85
"
Sample code as follows:
InetAddress vipAddress = InetAddress.getByAddress(DatatypeConverter.parseHexBinary("b019e85"));
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I write a small utility method to convert hex to ip(only ipv4). It does not do the validity check.
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;
}
java ip hex
I have a problem converting hex to IP address.
For example, I have two string "b019e85" an "ac12accf" which represent IP of "11.1.158.133" and "172.18.172.207". Is there a convenient way of converting such hex string to ip address?
I have read a number of answers, such as onvert-hexadecimal-string-to-ip-address and java-convert-int-to-inetaddress.
But both of them do not work. Either it throws exception of "java.net.UnknownHostException: addr is of illegal length" or "java.lang.IllegalArgumentException: hexBinary needs to be even-length: b019e85
"
Sample code as follows:
InetAddress vipAddress = InetAddress.getByAddress(DatatypeConverter.parseHexBinary("b019e85"));
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I write a small utility method to convert hex to ip(only ipv4). It does not do the validity check.
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;
}
java ip hex
java ip hex
edited Nov 21 '18 at 8:58
Ryder
asked Nov 21 '18 at 7:07
RyderRyder
135
135
the problem is in arguments: b019e85 is 7 digits. Needs to be 8. Even error says it...
– Sarief
Nov 21 '18 at 7:14
Make b019e85 into 0b019e85, then parse the parts of the string.. You can parse this with Integer.parseInt(hex,16),where hex is substring of the hex string, and construct the full IP address with StringBuilder afterwards. Out of my head, this is how I would do it
– Atizs
Nov 21 '18 at 7:15
posted an answer based on suggestion in comment from user43648
– daljian
Nov 21 '18 at 8:01
add a comment |
the problem is in arguments: b019e85 is 7 digits. Needs to be 8. Even error says it...
– Sarief
Nov 21 '18 at 7:14
Make b019e85 into 0b019e85, then parse the parts of the string.. You can parse this with Integer.parseInt(hex,16),where hex is substring of the hex string, and construct the full IP address with StringBuilder afterwards. Out of my head, this is how I would do it
– Atizs
Nov 21 '18 at 7:15
posted an answer based on suggestion in comment from user43648
– daljian
Nov 21 '18 at 8:01
the problem is in arguments: b019e85 is 7 digits. Needs to be 8. Even error says it...
– Sarief
Nov 21 '18 at 7:14
the problem is in arguments: b019e85 is 7 digits. Needs to be 8. Even error says it...
– Sarief
Nov 21 '18 at 7:14
Make b019e85 into 0b019e85, then parse the parts of the string.. You can parse this with Integer.parseInt(hex,16),where hex is substring of the hex string, and construct the full IP address with StringBuilder afterwards. Out of my head, this is how I would do it
– Atizs
Nov 21 '18 at 7:15
Make b019e85 into 0b019e85, then parse the parts of the string.. You can parse this with Integer.parseInt(hex,16),where hex is substring of the hex string, and construct the full IP address with StringBuilder afterwards. Out of my head, this is how I would do it
– Atizs
Nov 21 '18 at 7:15
posted an answer based on suggestion in comment from user43648
– daljian
Nov 21 '18 at 8:01
posted an answer based on suggestion in comment from user43648
– daljian
Nov 21 '18 at 8:01
add a comment |
1 Answer
1
active
oldest
votes
Not pretty, but should work.
public class IpTest {
public static void main(String args) throws Exception {
String hexAddrString1 = "b019e85";
String hexAddrString2 = "ac12accf";
System.out.println("ip:" + getIpFromHex(hexAddrString1));
System.out.println("ip:" + getIpFromHex(hexAddrString2));
//To get InetAddress
InetAddress vipAddress1 = InetAddress.getByName(getIpFromHex(hexAddrString1));
InetAddress vipAddress2 = InetAddress.getByName(getIpFromHex(hexAddrString2));
}
public static String getIpFromHex(String hexAddrString) {
if (hexAddrString.length() % 2 != 0) {
hexAddrString = "0" + hexAddrString;
}
if (hexAddrString.length() != 8) {
//error..
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hexAddrString.length(); i = i + 2) {
final String part = hexAddrString.substring(i, i + 2);
final int ipPart = Integer.parseInt(part, 16);
if (ipPart < 0 || ipPart > 254) {
//Error...
}
sb.append(ipPart);
if (i + 2 < hexAddrString.length()) {
sb.append(".");
}
}
return sb.toString();
}
}
I think it may work. I write a very small method in the post.
– Ryder
Nov 21 '18 at 9:04
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406882%2fhow-to-convert-a-hex-to-ip-address-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Not pretty, but should work.
public class IpTest {
public static void main(String args) throws Exception {
String hexAddrString1 = "b019e85";
String hexAddrString2 = "ac12accf";
System.out.println("ip:" + getIpFromHex(hexAddrString1));
System.out.println("ip:" + getIpFromHex(hexAddrString2));
//To get InetAddress
InetAddress vipAddress1 = InetAddress.getByName(getIpFromHex(hexAddrString1));
InetAddress vipAddress2 = InetAddress.getByName(getIpFromHex(hexAddrString2));
}
public static String getIpFromHex(String hexAddrString) {
if (hexAddrString.length() % 2 != 0) {
hexAddrString = "0" + hexAddrString;
}
if (hexAddrString.length() != 8) {
//error..
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hexAddrString.length(); i = i + 2) {
final String part = hexAddrString.substring(i, i + 2);
final int ipPart = Integer.parseInt(part, 16);
if (ipPart < 0 || ipPart > 254) {
//Error...
}
sb.append(ipPart);
if (i + 2 < hexAddrString.length()) {
sb.append(".");
}
}
return sb.toString();
}
}
I think it may work. I write a very small method in the post.
– Ryder
Nov 21 '18 at 9:04
add a comment |
Not pretty, but should work.
public class IpTest {
public static void main(String args) throws Exception {
String hexAddrString1 = "b019e85";
String hexAddrString2 = "ac12accf";
System.out.println("ip:" + getIpFromHex(hexAddrString1));
System.out.println("ip:" + getIpFromHex(hexAddrString2));
//To get InetAddress
InetAddress vipAddress1 = InetAddress.getByName(getIpFromHex(hexAddrString1));
InetAddress vipAddress2 = InetAddress.getByName(getIpFromHex(hexAddrString2));
}
public static String getIpFromHex(String hexAddrString) {
if (hexAddrString.length() % 2 != 0) {
hexAddrString = "0" + hexAddrString;
}
if (hexAddrString.length() != 8) {
//error..
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hexAddrString.length(); i = i + 2) {
final String part = hexAddrString.substring(i, i + 2);
final int ipPart = Integer.parseInt(part, 16);
if (ipPart < 0 || ipPart > 254) {
//Error...
}
sb.append(ipPart);
if (i + 2 < hexAddrString.length()) {
sb.append(".");
}
}
return sb.toString();
}
}
I think it may work. I write a very small method in the post.
– Ryder
Nov 21 '18 at 9:04
add a comment |
Not pretty, but should work.
public class IpTest {
public static void main(String args) throws Exception {
String hexAddrString1 = "b019e85";
String hexAddrString2 = "ac12accf";
System.out.println("ip:" + getIpFromHex(hexAddrString1));
System.out.println("ip:" + getIpFromHex(hexAddrString2));
//To get InetAddress
InetAddress vipAddress1 = InetAddress.getByName(getIpFromHex(hexAddrString1));
InetAddress vipAddress2 = InetAddress.getByName(getIpFromHex(hexAddrString2));
}
public static String getIpFromHex(String hexAddrString) {
if (hexAddrString.length() % 2 != 0) {
hexAddrString = "0" + hexAddrString;
}
if (hexAddrString.length() != 8) {
//error..
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hexAddrString.length(); i = i + 2) {
final String part = hexAddrString.substring(i, i + 2);
final int ipPart = Integer.parseInt(part, 16);
if (ipPart < 0 || ipPart > 254) {
//Error...
}
sb.append(ipPart);
if (i + 2 < hexAddrString.length()) {
sb.append(".");
}
}
return sb.toString();
}
}
Not pretty, but should work.
public class IpTest {
public static void main(String args) throws Exception {
String hexAddrString1 = "b019e85";
String hexAddrString2 = "ac12accf";
System.out.println("ip:" + getIpFromHex(hexAddrString1));
System.out.println("ip:" + getIpFromHex(hexAddrString2));
//To get InetAddress
InetAddress vipAddress1 = InetAddress.getByName(getIpFromHex(hexAddrString1));
InetAddress vipAddress2 = InetAddress.getByName(getIpFromHex(hexAddrString2));
}
public static String getIpFromHex(String hexAddrString) {
if (hexAddrString.length() % 2 != 0) {
hexAddrString = "0" + hexAddrString;
}
if (hexAddrString.length() != 8) {
//error..
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hexAddrString.length(); i = i + 2) {
final String part = hexAddrString.substring(i, i + 2);
final int ipPart = Integer.parseInt(part, 16);
if (ipPart < 0 || ipPart > 254) {
//Error...
}
sb.append(ipPart);
if (i + 2 < hexAddrString.length()) {
sb.append(".");
}
}
return sb.toString();
}
}
edited Nov 21 '18 at 8:07
answered Nov 21 '18 at 8:00
daljiandaljian
67111
67111
I think it may work. I write a very small method in the post.
– Ryder
Nov 21 '18 at 9:04
add a comment |
I think it may work. I write a very small method in the post.
– Ryder
Nov 21 '18 at 9:04
I think it may work. I write a very small method in the post.
– Ryder
Nov 21 '18 at 9:04
I think it may work. I write a very small method in the post.
– Ryder
Nov 21 '18 at 9:04
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406882%2fhow-to-convert-a-hex-to-ip-address-in-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
the problem is in arguments: b019e85 is 7 digits. Needs to be 8. Even error says it...
– Sarief
Nov 21 '18 at 7:14
Make b019e85 into 0b019e85, then parse the parts of the string.. You can parse this with Integer.parseInt(hex,16),where hex is substring of the hex string, and construct the full IP address with StringBuilder afterwards. Out of my head, this is how I would do it
– Atizs
Nov 21 '18 at 7:15
posted an answer based on suggestion in comment from user43648
– daljian
Nov 21 '18 at 8:01