Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 835
Rg90 Avatar asked Jul 05 '13 12:07

Rg90


People also ask

Can IP address be in hexadecimal?

An IP address can be expressed in dotted decimal, binary, octal, or hexadecimal. While all are correct and mean the same thing, it's most common to use dotted decimal notation for IPv4 and hexadecimal (hex) for IPv6.

Can you convert an XUID to an IP?

Users can also convert Hex File to IP by uploading the file. It also referred as XUID to IP Converter. Hex IP to Decimal converter works well on Windows, MAC, Linux, Chrome, Firefox, Edge, and Safari.


2 Answers

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.

like image 113
JREN Avatar answered Sep 29 '22 15:09

JREN


try this

InetAddress a = InetAddress.getByAddress(DatatypeConverter.parseHexBinary("0A064156"));

DatatypeConverter is from standard javax.xml.bind package

like image 25
Evgeniy Dorofeev Avatar answered Sep 29 '22 15:09

Evgeniy Dorofeev