1) Read the string character by character and convert it to int.
2) use Integer.toBinaryString() for the above
Main Topics
Browse All Topicsi want to convert string which is an ip address into binary and the string is with the subnet mask like / 27 and i need to convert this as well into binary and perform and operation on it
show me the entire code from the scrach and also with an example pls
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Say You have a string:
s = "64.54.223.107/27"
Then the substrings can be converted to numbers this way:
int mask = Integer.parseInt(s.substri
s = s.substring(0, s.indexOf('/'));
int[] ip = new int[4];
String[] subs = s.split("\\.");
for(int i = 0; i < subs.length; i++)
ip[i] = Integer.parseInt(subs[i]);
after this You will have an array 'ip', containing the 4 IP numbers and a 'mask'
If You want the line
"192.123.64.0/27"
to be converted to
"192.123.64.0"
using this function:
bitwise AND of 192.123.64.0 with /27 (255.255.255.224)
Then here is a sample application:
public class IP {
public static String f(String s) {
int mask = Integer.parseInt(s.substri
s = s.substring(0, s.indexOf('/'));
int[] ip = new int[4];
String[] subs = s.split("\\.");
for(int i = 0; i < subs.length; i++)
ip[i] = Integer.parseInt(subs[i]);
long l = ip[0] << 24 | ip[1] << 16 | ip[2] << 8 | ip[3];
long lmask = ((long)Math.pow(2, mask+1) - 1) << (32 - mask);
long result = l & lmask;
return "" + ((result >> 24) & 0xFF) + "." + ((result >> 16) & 0xFF) + "." + ((result >> 8) & 0xFF) + "." + (result & 0xFF);
}
public static void main(String[] args) {
String s = "64.54.223.107/27";
System.out.println(f(s));
s = "192.123.64.0/27";
System.out.println(f(s));
}
}
Business Accounts
Answer for Membership
by: deepen_antivirusPosted on 2006-08-29 at 15:55:32ID: 17415899
pls be quick its very urgent