Re: How to get a raw IP-addr into byte-array?
From: Sam Pinkerton (prescindor_at_ftml.net)
Date: 09/11/04
- Next message: Alex Kizub: "Re: How do I know what Java SDK I am running?"
- Previous message: Josh Eckerman: "Re: How do I know what Java SDK I am running?"
- In reply to: David Cook: "How to get a raw IP-addr into byte-array?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 11 Sep 2004 00:58:32 -0700
Well, say you had the address java.sun.com [209.249.116.143]. You
could pack that into a byte array thus:
byte[] addr = new byte[4];
addr[0] = (byte)209;
addr[1] = (byte)249;
addr[2] = (byte)116;
addr[3] = (byte)143;
Or you could just cram it all into an int, like so:
int addy = 209*16777216 + 249*65536 + 116*256 + 143;
System.out.println("java.sun.com = " + addy);
That prints out -772180849. But my browser doesn't like the sign, so
I need it unsigned:
System.out.println("java.sun.com = " +
((long)addy & 0xffffffffL));
That prints out 3522786447, and indeed, if I point my browser at
it does the right thing. Hey there, Gosling! We need unsigned!
BTW, Integer.getInteger looks up system properties and returns them as
Integer objects, assuming they exist and are convertible. If not, it
returns null, as you found out. The following will do what you want:
byte b = (byte)Integer.parseInt("192");
System.out.println(b);
Oops. That prints -64. There's that pesky sign problem again. So,
you could use
System.out.println(b & 255);
which indeed prints 192. What happens is that Java fetches the byte
and sign-extends it to an int, which it passes to the println method.
And'ing it with 255 gets rid of the extended sign bits. Of course, if
we had unsigned bytes, the promotion to int would supply zeros
instead, and the extra code would not be needed.
"David Cook" <(who wants to know?)> wrote in message news:<caGdnTGDtsSEi6TcRVn-iA@comcast.com>...
> Java's InetAddress class has some methods that use
> a byte-array to hold what it describes as a 'raw IP address'.
>
> So, I assume that they mean an array like:
> byte[] ba = new byte[4];
> would hold an IPv4 address.
>
> Ok, yes, in theory, there are enough bits to hold the values.
> But, my Java book clearly states that a byte is a SIGNED
> quantity, is part of the Integer class, and can hold values ranging from 127
> to -128.
> And, my book also states that there is no 'unsigned' keyword in Java.
>
> So, given that, how would I write code to fill each of the 4 array
> elements, when I have values as large as 255 (and never any negative
> values)?
>
> Let's say I want to StringToken-ize an IP-address from a string, to load
> up a byte-array with the 4 values.
> I have a string (IP-address) like:
> 192.168.0.1
> I parse out the first value (192), but when I use code like:
>
> String str = "192";
> Integer i = Integer.getInteger(str);
> byte b = i.byteValue(); //Causes NullPointerException
>
> So, what extra hoop do I need to leap thru to get each octet into
> its byte? (It probably should be obvious, but not to me.)
>
> TIA...
>
> Dave
- Next message: Alex Kizub: "Re: How do I know what Java SDK I am running?"
- Previous message: Josh Eckerman: "Re: How do I know what Java SDK I am running?"
- In reply to: David Cook: "How to get a raw IP-addr into byte-array?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|