Hm, I'm a bit reluctant to post here, because when I wrote my previous post 
on this thread I felt like letting off a bit of steam in defense of Bob. I 
couldn't see what's so horribly wrong with his solution given this 
particular context. 
I agree with what you say and in general you're right with your statement 
about using well-known and tested methods. And I also have to admit that I 
wasn't thinking of IPv6 because of that particular context:

The IP address returned by WifiInfo.getIpAddress() is a Java int. It cannot 
be an IPv6 number, because a Java int isn't big enough. Using bit shifting 
and string formatting on a 32 bits IP address number is a common technique 
that does not look obfuscated at all to me. And that is what happens under 
the hood anyway when calling the InetAddress.toString method.

However, you raised a good point about dealing with IPv6 and making your 
code future-proof. Unfortunately with the current design of the WifiInfo 
class it doesn't seem to be possible to get an IPv6 address. I guess there 
needs to be an extension of its interface to support that.


On Wednesday, February 20, 2013 4:11:42 AM UTC-6, Jxn wrote:
>
> Den tisdagen den 19:e februari 2013 kl. 18:30:45 UTC+1 skrev Nobu Games:
> > Bob was asking for the string representation of an integer IP number. 
> His solution is correct, straightforward and does not require the 
> instantiation of three intermediate objects just for getting a simple 
> string. Your one-liner however creates a BigInteger, then a byte array and 
> finally an instance of InetAddress which still is not a string. It's 
> toString method may or may not return the resolved host name in addition to 
> the IP number string representation. That's not quite the original 
> requirement.
> > 
>
> But his solution isn't that wrong. The "easy" solution are plain wrong if 
> you have something else than IPv4 addresses.
>
> > On Tuesday, February 19, 2013 9:43:35 AM UTC-6, Larry Meadors wrote:On 
> Tue, Feb 19, 2013 at 8:05 AM, bob <b...@coolfone.comze.com> wrote:
> > 
> > > Why do you say that?
> > 
> > 
> > 
> > Because it's a ridiculously obfuscated solution to a problem with a
> > 
> > simple one-line solution:
> > 
> > 
> > 
> > InetAddress address =
> > 
> InetAddress.getByAddress(BigInteger.valueOf(actualIpAddressAsInt).toByteArray());
> > 
>
> This uses a proper encapsulation at least, which the "simple" and "fast" 
> solution doesn't. I always prefer Slow and Right before Fast and Wrong.
>
> You should also allow the compiler to do the low level optimisation. Its 
> usually better than the programmer in doing that if you don't "optimize" 
> yourself. 
>
> Happy codeing.
>
>

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to