Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using indexOf and substring to calculate a number

Tags:

java

I'm not sure how to go about this, my program is supposed to get the user to input an IP address. the program is then supposed to return a value in decimal notation. e.g 134.115.64.1 is input and 2255699969 is output.

I was wondering if i could store 134,115,64 and 1 in variable. if so, how could i go about it? A link to any sort of tutorial would be great as i haven't managed to find one my self.

Thanks

like image 790
Eddy Freeman Avatar asked Dec 31 '25 02:12

Eddy Freeman


2 Answers

A trick for parsing IP addresses

BigInteger ip = new BigInteger(1,InetAddress.getByName("134.115.64.1").getAddress());
// or
long ip = InetAddress.getByName("134.115.64.1").hashCode() & 0xffffffffL;

System.out.println(ip);

prints

2255699969
like image 82
Peter Lawrey Avatar answered Jan 02 '26 16:01

Peter Lawrey


You can do something like :

String str = "134.115.64.1";

String arr[] = str.split("\\.");

int[] ips = new int[arr.length];
for(int i=0; i<arr.length;i++)
    ips[i] = Integer.parseInt(arr[i]);
like image 34
Nandkumar Tekale Avatar answered Jan 02 '26 16:01

Nandkumar Tekale



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!