Convert hexadecimal string to IP Address
I want to convert a string value (in hexadecimal) to an IP Address. How can I do it using Java?
Hex value: 0A064156
IP: 10.6.65.86
This site gives me the correct result, but I am not sure how to implement this in my code.
Can it be done directly in an XSLT?
java ip hex converters
add a comment |
I want to convert a string value (in hexadecimal) to an IP Address. How can I do it using Java?
Hex value: 0A064156
IP: 10.6.65.86
This site gives me the correct result, but I am not sure how to implement this in my code.
Can it be done directly in an XSLT?
java ip hex converters
1
1. Split the string into substrings of length 2. 2. Convert all substrings to dezimal. 3. Insert dots between all substrings.
– Sirko
Jul 5 '13 at 12:55
add a comment |
I want to convert a string value (in hexadecimal) to an IP Address. How can I do it using Java?
Hex value: 0A064156
IP: 10.6.65.86
This site gives me the correct result, but I am not sure how to implement this in my code.
Can it be done directly in an XSLT?
java ip hex converters
I want to convert a string value (in hexadecimal) to an IP Address. How can I do it using Java?
Hex value: 0A064156
IP: 10.6.65.86
This site gives me the correct result, but I am not sure how to implement this in my code.
Can it be done directly in an XSLT?
java ip hex converters
java ip hex converters
edited Nov 21 '18 at 11:47
asked Jul 5 '13 at 12:53
Rg90
3283826
3283826
1
1. Split the string into substrings of length 2. 2. Convert all substrings to dezimal. 3. Insert dots between all substrings.
– Sirko
Jul 5 '13 at 12:55
add a comment |
1
1. Split the string into substrings of length 2. 2. Convert all substrings to dezimal. 3. Insert dots between all substrings.
– Sirko
Jul 5 '13 at 12:55
1
1
1. Split the string into substrings of length 2. 2. Convert all substrings to dezimal. 3. Insert dots between all substrings.
– Sirko
Jul 5 '13 at 12:55
1. Split the string into substrings of length 2. 2. Convert all substrings to dezimal. 3. Insert dots between all substrings.
– Sirko
Jul 5 '13 at 12:55
add a comment |
6 Answers
6
active
oldest
votes
try this
InetAddress a = InetAddress.getByAddress(DatatypeConverter.parseHexBinary("0A064156"));
DatatypeConverter is from standard javax.xml.bind
package
1
+1. I really like this answer. Not a lot of code or string manipulation and gives you exactly what you want.
– ARC
Jul 5 '13 at 13:15
Thanks a lot! Sweet and precise.
– Rg90
Jul 5 '13 at 13:45
Note: SinceJava 9
DatatypeConverter
is no longer part of the standard: github.com/http-kit/http-kit/issues/356
– BullyWiiPlaza
Jun 7 '18 at 21:32
add a comment |
You can split your hex value in groups of 2 and then convert them to integers.
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.
1
plus voted,Your answer actually teaches how to catch fish
– HRgiger
Oct 14 '15 at 11:21
Good answer, just a typo. It should behexValue.substring
. docs.oracle.com/javase/7/docs/api/java/lang/…
– Paul
Nov 15 '18 at 1:02
add a comment |
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();
}
add a comment |
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;
}
add a comment |
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;
}
add a comment |
You can split it 2 characters, and then use Integer.parse(string, radix) to convert to integer values
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html#parseInt(java.lang.String,int)
The link is dead.
– user2796515
Oct 10 '18 at 15:02
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%2f17489404%2fconvert-hexadecimal-string-to-ip-address%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
try this
InetAddress a = InetAddress.getByAddress(DatatypeConverter.parseHexBinary("0A064156"));
DatatypeConverter is from standard javax.xml.bind
package
1
+1. I really like this answer. Not a lot of code or string manipulation and gives you exactly what you want.
– ARC
Jul 5 '13 at 13:15
Thanks a lot! Sweet and precise.
– Rg90
Jul 5 '13 at 13:45
Note: SinceJava 9
DatatypeConverter
is no longer part of the standard: github.com/http-kit/http-kit/issues/356
– BullyWiiPlaza
Jun 7 '18 at 21:32
add a comment |
try this
InetAddress a = InetAddress.getByAddress(DatatypeConverter.parseHexBinary("0A064156"));
DatatypeConverter is from standard javax.xml.bind
package
1
+1. I really like this answer. Not a lot of code or string manipulation and gives you exactly what you want.
– ARC
Jul 5 '13 at 13:15
Thanks a lot! Sweet and precise.
– Rg90
Jul 5 '13 at 13:45
Note: SinceJava 9
DatatypeConverter
is no longer part of the standard: github.com/http-kit/http-kit/issues/356
– BullyWiiPlaza
Jun 7 '18 at 21:32
add a comment |
try this
InetAddress a = InetAddress.getByAddress(DatatypeConverter.parseHexBinary("0A064156"));
DatatypeConverter is from standard javax.xml.bind
package
try this
InetAddress a = InetAddress.getByAddress(DatatypeConverter.parseHexBinary("0A064156"));
DatatypeConverter is from standard javax.xml.bind
package
edited Jul 5 '13 at 13:04
answered Jul 5 '13 at 12:57
Evgeniy Dorofeev
105k23140219
105k23140219
1
+1. I really like this answer. Not a lot of code or string manipulation and gives you exactly what you want.
– ARC
Jul 5 '13 at 13:15
Thanks a lot! Sweet and precise.
– Rg90
Jul 5 '13 at 13:45
Note: SinceJava 9
DatatypeConverter
is no longer part of the standard: github.com/http-kit/http-kit/issues/356
– BullyWiiPlaza
Jun 7 '18 at 21:32
add a comment |
1
+1. I really like this answer. Not a lot of code or string manipulation and gives you exactly what you want.
– ARC
Jul 5 '13 at 13:15
Thanks a lot! Sweet and precise.
– Rg90
Jul 5 '13 at 13:45
Note: SinceJava 9
DatatypeConverter
is no longer part of the standard: github.com/http-kit/http-kit/issues/356
– BullyWiiPlaza
Jun 7 '18 at 21:32
1
1
+1. I really like this answer. Not a lot of code or string manipulation and gives you exactly what you want.
– ARC
Jul 5 '13 at 13:15
+1. I really like this answer. Not a lot of code or string manipulation and gives you exactly what you want.
– ARC
Jul 5 '13 at 13:15
Thanks a lot! Sweet and precise.
– Rg90
Jul 5 '13 at 13:45
Thanks a lot! Sweet and precise.
– Rg90
Jul 5 '13 at 13:45
Note: Since
Java 9
DatatypeConverter
is no longer part of the standard: github.com/http-kit/http-kit/issues/356– BullyWiiPlaza
Jun 7 '18 at 21:32
Note: Since
Java 9
DatatypeConverter
is no longer part of the standard: github.com/http-kit/http-kit/issues/356– BullyWiiPlaza
Jun 7 '18 at 21:32
add a comment |
You can split your hex value in groups of 2 and then convert them to integers.
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.
1
plus voted,Your answer actually teaches how to catch fish
– HRgiger
Oct 14 '15 at 11:21
Good answer, just a typo. It should behexValue.substring
. docs.oracle.com/javase/7/docs/api/java/lang/…
– Paul
Nov 15 '18 at 1:02
add a comment |
You can split your hex value in groups of 2 and then convert them to integers.
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.
1
plus voted,Your answer actually teaches how to catch fish
– HRgiger
Oct 14 '15 at 11:21
Good answer, just a typo. It should behexValue.substring
. docs.oracle.com/javase/7/docs/api/java/lang/…
– Paul
Nov 15 '18 at 1:02
add a comment |
You can split your hex value in groups of 2 and then convert them to integers.
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.
You can split your hex value in groups of 2 and then convert them to integers.
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.
answered Jul 5 '13 at 13:00
JREN
2,83022142
2,83022142
1
plus voted,Your answer actually teaches how to catch fish
– HRgiger
Oct 14 '15 at 11:21
Good answer, just a typo. It should behexValue.substring
. docs.oracle.com/javase/7/docs/api/java/lang/…
– Paul
Nov 15 '18 at 1:02
add a comment |
1
plus voted,Your answer actually teaches how to catch fish
– HRgiger
Oct 14 '15 at 11:21
Good answer, just a typo. It should behexValue.substring
. docs.oracle.com/javase/7/docs/api/java/lang/…
– Paul
Nov 15 '18 at 1:02
1
1
plus voted,Your answer actually teaches how to catch fish
– HRgiger
Oct 14 '15 at 11:21
plus voted,Your answer actually teaches how to catch fish
– HRgiger
Oct 14 '15 at 11:21
Good answer, just a typo. It should be
hexValue.substring
. docs.oracle.com/javase/7/docs/api/java/lang/…– Paul
Nov 15 '18 at 1:02
Good answer, just a typo. It should be
hexValue.substring
. docs.oracle.com/javase/7/docs/api/java/lang/…– Paul
Nov 15 '18 at 1:02
add a comment |
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();
}
add a comment |
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();
}
add a comment |
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();
}
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();
}
answered Jul 5 '13 at 13:05
Devi Kiran
4281416
4281416
add a comment |
add a comment |
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;
}
add a comment |
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;
}
add a comment |
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;
}
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;
}
answered Jul 5 '13 at 13:09
Rahul Bobhate
3,38921442
3,38921442
add a comment |
add a comment |
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;
}
add a comment |
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;
}
add a comment |
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;
}
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;
}
answered Nov 21 '18 at 9:53
Ryder
64
64
add a comment |
add a comment |
You can split it 2 characters, and then use Integer.parse(string, radix) to convert to integer values
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html#parseInt(java.lang.String,int)
The link is dead.
– user2796515
Oct 10 '18 at 15:02
add a comment |
You can split it 2 characters, and then use Integer.parse(string, radix) to convert to integer values
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html#parseInt(java.lang.String,int)
The link is dead.
– user2796515
Oct 10 '18 at 15:02
add a comment |
You can split it 2 characters, and then use Integer.parse(string, radix) to convert to integer values
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html#parseInt(java.lang.String,int)
You can split it 2 characters, and then use Integer.parse(string, radix) to convert to integer values
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html#parseInt(java.lang.String,int)
answered Jul 5 '13 at 12:55
The Tosters
132111
132111
The link is dead.
– user2796515
Oct 10 '18 at 15:02
add a comment |
The link is dead.
– user2796515
Oct 10 '18 at 15:02
The link is dead.
– user2796515
Oct 10 '18 at 15:02
The link is dead.
– user2796515
Oct 10 '18 at 15:02
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f17489404%2fconvert-hexadecimal-string-to-ip-address%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
1
1. Split the string into substrings of length 2. 2. Convert all substrings to dezimal. 3. Insert dots between all substrings.
– Sirko
Jul 5 '13 at 12:55