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?
> 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
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);
> 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
> I read it somewhere, don't know where anymore. Like kajbj said, there is indeed another type of IP address: IPv6, 128 bits long.