Does anyone knows how to convert decimal notation of an IP address into binary form in Java? Please let me know...
相关问题
- IPAddress.[Try]Parse parses 192.168 to 192.0.0.168
- Name for a method that has only side effects
- Doing ARP and Inverse ARP on Linux 2.6.21 (glibc 2
- Best way to keep the user-interface up-to-date?
- should I write more descriptive function names or
相关文章
- Should client-server code be written in one “proje
- How to resolve hostname to an ip address in node j
- Algorithm for maximizing coverage of rectangular a
- Is there an existing solution for these particular
- What is Scope Creep? [closed]
- How can I modify .xfdl files? (Update #1)
- Parsing IP Address to string doesn't work for
- What is the best algorithm to shuffle cards? [clos
Gathering your suggestions and some other sources, I found usefull to convert an InetAdress to an array of bit, as well as BitSet, which can help to compute and(), or(), xor() out of your binary representation.
Following sample shows how to convert ip to binary and binary to ip.
Enjoy!
}
You can use the java.net.InetAddress class. Two methods you should look at are getByName and getAddress. Here is a simple code example
The open-source IPAddress Java library can do this for you. It can parse various IP address formats, including either IPv4 or IPv6, and has methods to produce various string formats, including one for binary. Disclaimer: I am the project manager of the IPAddress library.
This code will do it:
Example:
The output is:
An IP address written as
a.b.c.d
can be converted to a 32-bit integer valueusing shift and bit-wise inclusive OR operators as,
To be safe, each of
a,b,c,d
has valid range0-255
-- you can check that in your conversion.You can further validate the IP address using this regex example.