byte length problem

I don't understand this, but I suppose the solution is simple.

I wanna use the getByAddress(byte[] address) method of the class InetAddress in java.net .

address is a byte array of length 4, that should contain the 4 parts of an IP address.

BUT, each part of an IP address can be 32 bits long, wich is bigger then the possible length of a byte.

Someone knows what I do wrong?

[411 byte] By [pieterblommea] at [2007-10-2 10:00:58]
# 1

> I don't understand this, but I suppose the solution

> is simple.

> I wanna use the getByAddress(byte[] address) method

> of the class InetAddress in java.net .

> address is a byte array of length 4, that should

> contain the 4 parts of an IP address.

> BUT, each part of an IP address can be 32 bits long,

> wich is bigger then the possible length of a byte.

> Someone knows what I do wrong?

Hi, it depends on which type of ip-address. The one which is most common is only 32 bits long. E.g. 255.123.123.01 <- 4 bytes.

Kaj

kajbja at 2007-7-13 1:11:39 > top of Java-index,Java Essentials,Java Programming...
# 2
> BUT, each part of an IP address can be 32 bits long,Where did you get that idea?0-255 seems to me a lot like 8 bits long.
jfbrierea at 2007-7-13 1:11:39 > top of Java-index,Java Essentials,Java Programming...
# 3

I read it somewhere, don't know where anymore.

So, you mean it shouldn't be a problem?

I do get an the message: possible loss of precision if I use this:

byte[] test = new byte[4];

test[0] = 192;

test[1] = 168;

test[2] = 0;

test[3] = 52;

InetAddress.getByAddress(test);

pieterblommea at 2007-7-13 1:11:39 > top of Java-index,Java Essentials,Java Programming...
# 4

> I read it somewhere, don't know where anymore.

> So, you mean it shouldn't be a problem?

> I do get an the message: possible loss of precision

> if I use this:

> byte[] test = new byte[4];

> test[0] = 192;

> test[1] = 168;

> test[2] = 0;

> test[3] = 52;

> InetAddress.getByAddress(test);

That's bytes in java are signed and have values in range -128 to 127.

Kaj

kajbja at 2007-7-13 1:11:39 > top of Java-index,Java Essentials,Java Programming...
# 5
> That's because bytes in java are signed and have values in> range -128 to 127.> > Kaj
kajbja at 2007-7-13 1:11:40 > top of Java-index,Java Essentials,Java Programming...
# 6
and how do I solve the problem?
pieterblommea at 2007-7-13 1:11:40 > top of Java-index,Java Essentials,Java Programming...
# 7
> I read it somewhere, don't know where anymore. Like kajbj said, there is indeed another type of IP address: IPv6, 128 bits long.
jfbrierea at 2007-7-13 1:11:40 > top of Java-index,Java Essentials,Java Programming...
# 8
test[0] = (byte)192;
jverda at 2007-7-13 1:11:40 > top of Java-index,Java Essentials,Java Programming...
# 9
ok, thanks
pieterblommea at 2007-7-13 1:11:40 > top of Java-index,Java Essentials,Java Programming...