Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ipv6 to numeric format

 
Miran Cvenkel
Ranch Hand
Posts: 202
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ipv4 to numeric (this converts 127.0.0.1 to some integer, how to do that for0:0:0:0:0:0:0:1) ?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happened when you tried to give input as 0:0:0:0:0:0:0:1?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before you write any code, you need to decide what might need changing, so you need a list of differences between IPV4 and IPV6
I'll start you off

IPV6 uses : as a separator instead of .
 
Miran Cvenkel
Ranch Hand
Posts: 202
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:What happened when you tried to give input as 0:0:0:0:0:0:0:1?


unforeseen exception, otherwise the problem would not arise at all, obviously.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay The existing code works with '.' as a separator and counter is assigned to 3 for ipv4. Do you intend to handle ipv6 too in the same method or in a separate one?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Miran Cvenkel wrote:
John Jai wrote:What happened when you tried to give input as 0:0:0:0:0:0:0:1?


unforeseen exception, otherwise the problem would not arise at all, obviously.


No, it's not obvious at all. There are many different things that could go wrong.

For instance: How many bits in an IPv4 address? How many bits in an IPv6 address? How many bits in an int? How many bits in a long?

Also, you definitely don't want to use math.pow() to shortcut integer multiplication, or bring doubles into it in any way. There are values that long can represent that double can't, so the double version of a long may lose precision--you may not end up with the value you expect.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic