By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,266 Members | 1,816 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,266 IT Pros & Developers. It's quick & easy.

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

P: n/a
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


Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
I'm not 100% sure why you need to do this. You can pass an IP address
as-is to the InetAddress.getByName() method. You don't HAVE to give it a
DNS name.

That said, if you really still want to go down this road, I did create a
simple utility class which does bitwise casting between Integers and Bytes:

/*
* BitwiseCast.java
*
* Created on May 11, 2002, 9:21 PM
*/

package com.webcorp.util;

/** This class provides methods to casting values. For example, if we
* were attemptin to cast a byte of 0xFF, the number would be a NEGATIVE
* integer, which is incorrect. The integer should just be 255.
*
* @author Nathan Crause
* @version
*/
public class BitwiseCast {
public static int byte2int(byte b) {
int ret = 0;
byte work = b;

for (int i = 0; i < 8; ++i) {
ret <<= 1;
if ((work & 0x80) > 0) ret |= 0x01;
work <<= 1;
}

return ret;
}

public static byte int2byte(int b) {
byte ret = 0;
int work = b;

for (int i = 0; i < 8; ++i) {
ret <<= 1;
if ((work & 0x80) > 0) ret |= 0x01;
work <<= 1;
}

return ret;
}

public static void main(String args[]) {
int x = 200;
byte y = -100;
System.out.println(x + "=" + int2byte(x));
System.out.println(y + "=" + byte2int(y));
}
}

I hope this helps.


David Cook wrote:
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

Jul 17 '05 #2

P: n/a
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:<ca********************@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

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.