jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes Java code for Padding Zero to IP Address : 01.21.03.004 to 001.021.003.004 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java code for Padding Zero to IP Address : 01.21.03.004 to 001.021.003.004" Watch "Java code for Padding Zero to IP Address : 01.21.03.004 to 001.021.003.004" New topic
Author

Java code for Padding Zero to IP Address : 01.21.03.004 to 001.021.003.004

Rahul Naik
Greenhorn

Joined: May 14, 2012
Posts: 4

Dear Sir,

Please tel me java code how to padd zero to IP Address.
For example : i have received ip address as 01.21.03.004 and now i would like pad zero to the received ip address for form full 15 digit ip address as 001.021.003.004
Advice java code for this

Thanks
Rahul
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
1) split the string into parts according to the dots
2) pad every part
3) lump them together

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Rahul Naik wrote:For example : i have received ip address as 01.21.03.004 and now i would like pad zero to the received ip address for form full 15 digit ip address as 001.021.003.004

And just to add to Ivan's good advice, I'd be tempted to convert those 4 numeric strings to actual numbers - and maybe even creating an IPAddress class to hold them - because that's what they are.

As for displaying them: Have a look a String's format() method. It contains all sorts of controls for exactly this kind of stuff.

It should be added that what you're getting looks like an IPv4 address. You might also want to think about what you do if you get an IPv6 one.

Advice java code for this

'Fraid that's not how it works here. ShowSomeEffort (←click) and we'll be happy to help if you run into problems.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java code for Padding Zero to IP Address : 01.21.03.004 to 001.021.003.004