Re: How to get a raw IP-addr into byte-array?

From: Sam Pinkerton (prescindor_at_ftml.net)
Date: 09/11/04


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

     http://3522786447

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



Relevant Pages

  • Re: 2 GIG the new 640 MB
    ... >>program will ever be able to create an array with more than 4 billion ... > autocasting it back to int when required. ... Java doesn't even see pointers -- unless one looks carefully. ...
    (comp.lang.java.advocacy)
  • Re: method parameters
    ... This is my second program with the JAVA, I need to use it to write ... The basic idiom for emulating pass by reference is to wrap the value in an array. ... public void someMethod(int w) ... public void resetSize(int x, int y) { ...
    (comp.lang.java.programmer)
  • Re: 2 GIG the new 640 MB
    ... >program will ever be able to create an array with more than 4 billion ... Java will need to undergo a transition to 64 bit like any ... autocasting it back to int when required. ... http://mindprod.com Java custom programming, consulting and coaching. ...
    (comp.lang.java.advocacy)
  • Re: How much fast is C++ than Java?
    ... What you'll find is that the Java indexing rules are nothing like as ... multidimensional array is an array of array objects. ... int main ...
    (comp.lang.c)
  • (patch for Bash) regex case statement
    ... Following up on my previous patch for regex conditional tests, ... /* Return an array of strings; ... int dollarflag, zeropad, compareflag; ... SHELL_VAR *var; ...
    (comp.unix.shell)