How to get Subnet from an IP in JavaCalling a function of a module by using its name (a string)Is Java “pass-by-reference” or “pass-by-value”?How do I check whether a file exists without exceptions?How do I read / convert an InputStream into a String in Java?How do I generate random integers within a specific range in Java?Convert bytes to a string?How do I sort a dictionary by value?Interface vs Abstract Class (general OO)What is a JavaBean exactly?How to fix java.lang.UnsupportedClassVersionError: Unsupported major.minor version
How long would it take for people to notice a mass disappearance?
I'm in your subnets, golfing your code
Set collection doesn't always enforce uniqueness with the Date datatype? Does the following example seem correct?
How to model the curly cable part of the phone
Would the Disguise Self spell be able to reveal hidden birthmarks/tattoos (of the person they're disguised as) to a character?
Send iMessage from Firefox
Why is Arya visibly scared in the library in S8E3?
How wide is a neg symbol, how to get the width for alignment?
Why wasn't the Night King naked in S08E03?
Verb "geeitet" in an old scientific text
What are the differences between credential stuffing and password spraying?
What happens if you dump antimatter into a black hole?
How do I overfit?
Why doesn't WotC use established keywords on all new cards?
Why isn't nylon as strong as kevlar?
Missing Piece of Pie - Can you find it?
What is the difference between 'unconcealed' and 'revealed'?
Purpose of のは in this sentence?
Why do money exchangers give different rates to different bills?
How was the quadratic formula created?
Why do only some White Walkers shatter into ice chips?
Shantae Dance Matching
What was the design of the Macintosh II's MMU replacement?
Manager is threatening to grade me poorly if I don't complete the project
How to get Subnet from an IP in Java
Calling a function of a module by using its name (a string)Is Java “pass-by-reference” or “pass-by-value”?How do I check whether a file exists without exceptions?How do I read / convert an InputStream into a String in Java?How do I generate random integers within a specific range in Java?Convert bytes to a string?How do I sort a dictionary by value?Interface vs Abstract Class (general OO)What is a JavaBean exactly?How to fix java.lang.UnsupportedClassVersionError: Unsupported major.minor version
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
How do I get the subnet value from an IP Address.
For eg, we can do below in Python. (But how about Java?)
interface = ipaddress.IPv4Interface('11.22.104.0/0.0.0.255')
value = interface.with_prefixlen
Output of value = 11.22.104.0/24
Basically, I need a function to convert 0.0.0.255 into /24. Thanks.
Edit
-Java 7 is used.
java python interface ip subnet
add a comment |
How do I get the subnet value from an IP Address.
For eg, we can do below in Python. (But how about Java?)
interface = ipaddress.IPv4Interface('11.22.104.0/0.0.0.255')
value = interface.with_prefixlen
Output of value = 11.22.104.0/24
Basically, I need a function to convert 0.0.0.255 into /24. Thanks.
Edit
-Java 7 is used.
java python interface ip subnet
add a comment |
How do I get the subnet value from an IP Address.
For eg, we can do below in Python. (But how about Java?)
interface = ipaddress.IPv4Interface('11.22.104.0/0.0.0.255')
value = interface.with_prefixlen
Output of value = 11.22.104.0/24
Basically, I need a function to convert 0.0.0.255 into /24. Thanks.
Edit
-Java 7 is used.
java python interface ip subnet
How do I get the subnet value from an IP Address.
For eg, we can do below in Python. (But how about Java?)
interface = ipaddress.IPv4Interface('11.22.104.0/0.0.0.255')
value = interface.with_prefixlen
Output of value = 11.22.104.0/24
Basically, I need a function to convert 0.0.0.255 into /24. Thanks.
Edit
-Java 7 is used.
java python interface ip subnet
java python interface ip subnet
edited Dec 21 '16 at 7:30
user1841495
asked Dec 21 '16 at 6:32
user1841495user1841495
306
306
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
With Guava:
int slash = Integer.bitCount(
~Ints.fromByteArray(
InetAddresses.forString("0.0.0.255").getAddress()));
With Java 8:
int slash = Arrays.stream("0.0.0.255".split("\."))
.mapToInt(Integer::parseInt)
.map(i -> ~i & 0xFF)
.map(Integer::bitCount)
.sum();
With Java 7:
int slash = 0;
for (String octet : "0.0.0.255".split("\."))
slash += Integer.bitCount(~Integer.parseInt(octet) & 0xFF);
The value of slash
will be 24.
Is there any ready-to-use library in Java 7? My application is using Java 7, hence the code provided above is not working. Sorry for never mentioning the Java version.
– user1841495
Dec 21 '16 at 7:29
@user1841495 Added a Java 7 version.
– shmosel
Dec 21 '16 at 7:33
Thanks for the solution for Java 7, do you mind to make some explanation? I'm going to test with this solution shortly. Thanks a lot.
– user1841495
Dec 21 '16 at 10:19
@user1841495 Basically we're converting the IP address to 4 unsigned bytes, then counting the inverted bits (because this is a wildcard mask).
– shmosel
Dec 21 '16 at 18:09
add a comment |
The IPAddress Java library supports both IPv4 and IPv6 subnets in a polymorphic manner. Disclaimer: I am the project manager. The following code will convert a host mask to a prefix length:
IPAddressString maskString = new IPAddressString("0.0.0.255");
IPAddress mask = maskString.toAddress();
Integer prefLen = mask.getBlockMaskPrefixLength(false);//24
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%2f41256515%2fhow-to-get-subnet-from-an-ip-in-java%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
With Guava:
int slash = Integer.bitCount(
~Ints.fromByteArray(
InetAddresses.forString("0.0.0.255").getAddress()));
With Java 8:
int slash = Arrays.stream("0.0.0.255".split("\."))
.mapToInt(Integer::parseInt)
.map(i -> ~i & 0xFF)
.map(Integer::bitCount)
.sum();
With Java 7:
int slash = 0;
for (String octet : "0.0.0.255".split("\."))
slash += Integer.bitCount(~Integer.parseInt(octet) & 0xFF);
The value of slash
will be 24.
Is there any ready-to-use library in Java 7? My application is using Java 7, hence the code provided above is not working. Sorry for never mentioning the Java version.
– user1841495
Dec 21 '16 at 7:29
@user1841495 Added a Java 7 version.
– shmosel
Dec 21 '16 at 7:33
Thanks for the solution for Java 7, do you mind to make some explanation? I'm going to test with this solution shortly. Thanks a lot.
– user1841495
Dec 21 '16 at 10:19
@user1841495 Basically we're converting the IP address to 4 unsigned bytes, then counting the inverted bits (because this is a wildcard mask).
– shmosel
Dec 21 '16 at 18:09
add a comment |
With Guava:
int slash = Integer.bitCount(
~Ints.fromByteArray(
InetAddresses.forString("0.0.0.255").getAddress()));
With Java 8:
int slash = Arrays.stream("0.0.0.255".split("\."))
.mapToInt(Integer::parseInt)
.map(i -> ~i & 0xFF)
.map(Integer::bitCount)
.sum();
With Java 7:
int slash = 0;
for (String octet : "0.0.0.255".split("\."))
slash += Integer.bitCount(~Integer.parseInt(octet) & 0xFF);
The value of slash
will be 24.
Is there any ready-to-use library in Java 7? My application is using Java 7, hence the code provided above is not working. Sorry for never mentioning the Java version.
– user1841495
Dec 21 '16 at 7:29
@user1841495 Added a Java 7 version.
– shmosel
Dec 21 '16 at 7:33
Thanks for the solution for Java 7, do you mind to make some explanation? I'm going to test with this solution shortly. Thanks a lot.
– user1841495
Dec 21 '16 at 10:19
@user1841495 Basically we're converting the IP address to 4 unsigned bytes, then counting the inverted bits (because this is a wildcard mask).
– shmosel
Dec 21 '16 at 18:09
add a comment |
With Guava:
int slash = Integer.bitCount(
~Ints.fromByteArray(
InetAddresses.forString("0.0.0.255").getAddress()));
With Java 8:
int slash = Arrays.stream("0.0.0.255".split("\."))
.mapToInt(Integer::parseInt)
.map(i -> ~i & 0xFF)
.map(Integer::bitCount)
.sum();
With Java 7:
int slash = 0;
for (String octet : "0.0.0.255".split("\."))
slash += Integer.bitCount(~Integer.parseInt(octet) & 0xFF);
The value of slash
will be 24.
With Guava:
int slash = Integer.bitCount(
~Ints.fromByteArray(
InetAddresses.forString("0.0.0.255").getAddress()));
With Java 8:
int slash = Arrays.stream("0.0.0.255".split("\."))
.mapToInt(Integer::parseInt)
.map(i -> ~i & 0xFF)
.map(Integer::bitCount)
.sum();
With Java 7:
int slash = 0;
for (String octet : "0.0.0.255".split("\."))
slash += Integer.bitCount(~Integer.parseInt(octet) & 0xFF);
The value of slash
will be 24.
edited Dec 21 '16 at 7:32
answered Dec 21 '16 at 7:21
shmoselshmosel
37.2k43997
37.2k43997
Is there any ready-to-use library in Java 7? My application is using Java 7, hence the code provided above is not working. Sorry for never mentioning the Java version.
– user1841495
Dec 21 '16 at 7:29
@user1841495 Added a Java 7 version.
– shmosel
Dec 21 '16 at 7:33
Thanks for the solution for Java 7, do you mind to make some explanation? I'm going to test with this solution shortly. Thanks a lot.
– user1841495
Dec 21 '16 at 10:19
@user1841495 Basically we're converting the IP address to 4 unsigned bytes, then counting the inverted bits (because this is a wildcard mask).
– shmosel
Dec 21 '16 at 18:09
add a comment |
Is there any ready-to-use library in Java 7? My application is using Java 7, hence the code provided above is not working. Sorry for never mentioning the Java version.
– user1841495
Dec 21 '16 at 7:29
@user1841495 Added a Java 7 version.
– shmosel
Dec 21 '16 at 7:33
Thanks for the solution for Java 7, do you mind to make some explanation? I'm going to test with this solution shortly. Thanks a lot.
– user1841495
Dec 21 '16 at 10:19
@user1841495 Basically we're converting the IP address to 4 unsigned bytes, then counting the inverted bits (because this is a wildcard mask).
– shmosel
Dec 21 '16 at 18:09
Is there any ready-to-use library in Java 7? My application is using Java 7, hence the code provided above is not working. Sorry for never mentioning the Java version.
– user1841495
Dec 21 '16 at 7:29
Is there any ready-to-use library in Java 7? My application is using Java 7, hence the code provided above is not working. Sorry for never mentioning the Java version.
– user1841495
Dec 21 '16 at 7:29
@user1841495 Added a Java 7 version.
– shmosel
Dec 21 '16 at 7:33
@user1841495 Added a Java 7 version.
– shmosel
Dec 21 '16 at 7:33
Thanks for the solution for Java 7, do you mind to make some explanation? I'm going to test with this solution shortly. Thanks a lot.
– user1841495
Dec 21 '16 at 10:19
Thanks for the solution for Java 7, do you mind to make some explanation? I'm going to test with this solution shortly. Thanks a lot.
– user1841495
Dec 21 '16 at 10:19
@user1841495 Basically we're converting the IP address to 4 unsigned bytes, then counting the inverted bits (because this is a wildcard mask).
– shmosel
Dec 21 '16 at 18:09
@user1841495 Basically we're converting the IP address to 4 unsigned bytes, then counting the inverted bits (because this is a wildcard mask).
– shmosel
Dec 21 '16 at 18:09
add a comment |
The IPAddress Java library supports both IPv4 and IPv6 subnets in a polymorphic manner. Disclaimer: I am the project manager. The following code will convert a host mask to a prefix length:
IPAddressString maskString = new IPAddressString("0.0.0.255");
IPAddress mask = maskString.toAddress();
Integer prefLen = mask.getBlockMaskPrefixLength(false);//24
add a comment |
The IPAddress Java library supports both IPv4 and IPv6 subnets in a polymorphic manner. Disclaimer: I am the project manager. The following code will convert a host mask to a prefix length:
IPAddressString maskString = new IPAddressString("0.0.0.255");
IPAddress mask = maskString.toAddress();
Integer prefLen = mask.getBlockMaskPrefixLength(false);//24
add a comment |
The IPAddress Java library supports both IPv4 and IPv6 subnets in a polymorphic manner. Disclaimer: I am the project manager. The following code will convert a host mask to a prefix length:
IPAddressString maskString = new IPAddressString("0.0.0.255");
IPAddress mask = maskString.toAddress();
Integer prefLen = mask.getBlockMaskPrefixLength(false);//24
The IPAddress Java library supports both IPv4 and IPv6 subnets in a polymorphic manner. Disclaimer: I am the project manager. The following code will convert a host mask to a prefix length:
IPAddressString maskString = new IPAddressString("0.0.0.255");
IPAddress mask = maskString.toAddress();
Integer prefLen = mask.getBlockMaskPrefixLength(false);//24
answered Mar 22 at 22:17
Sean FSean F
2,096614
2,096614
add a comment |
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%2f41256515%2fhow-to-get-subnet-from-an-ip-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