问题
I would like to convert a java.net.InetAddress
and I fight with the signed / unsigned problems. Such a pain.
I read convert from short to byte and viceversa in Java and Why byte b = (byte) 0xFF is equals to integer -1?
And as a result came up with:
final byte [] pumpeIPAddressRaw =
java.net.InetAddress.getByName (pumpeIPAddressName).getAddress ();
final long pumpeIPAddress =
((pumpeIPAddressRaw [0] & 0xFF) << (3*8)) +
((pumpeIPAddressRaw [1] & 0xFF) << (2*8)) +
((pumpeIPAddressRaw [2] & 0xFF) << (1*8)) +
(pumpeIPAddressRaw [3] & 0xFF);
android.util.Log.i (
Application.TAG, "LOG00120: Setzte Pumpen Addresse : " +
pumpeIPAddress + ":" + pumpeIPPort);
And guess what the log still shows:
04-10 13:12:07.398 I/ch.XXXX.remote.Application(24452): LOG00120: Setzte Pumpen Addresse : -1063035647:27015
Does anybody know what I am still doing wrong?
回答1:
& 0xff
blocks sign extension during conversion from byte
to int
, but your expression also contains conversion from int
to long
and you need to block sign extension during this conversion as well:
final long pumpeIPAddress =
(((pumpeIPAddressRaw [0] & 0xFF) << (3*8)) +
((pumpeIPAddressRaw [1] & 0xFF) << (2*8)) +
((pumpeIPAddressRaw [2] & 0xFF) << (1*8)) +
(pumpeIPAddressRaw [3] & 0xFF)) & 0xffffffffl;
Alternatively, you can convert from byte
to long
in a single step, by marking the second operand of & 0xff
operation as long
using l
suffix:
final long pumpeIPAddress =
((pumpeIPAddressRaw [0] & 0xFFl) << (3*8)) +
((pumpeIPAddressRaw [1] & 0xFFl) << (2*8)) +
((pumpeIPAddressRaw [2] & 0xFFl) << (1*8)) +
(pumpeIPAddressRaw [3] & 0xFFl);
回答2:
String ip = "127.0.0.1";
InetAddress inetAddress = InetAddress.getByName(ip);
// ByteOrder.BIG_ENDIAN by default
ByteBuffer buffer = ByteBuffer.allocate(Long.SIZE);
buffer.put(inetAddress.getAddress());
buffer.position(0);
Long longValue = buffer.getLong();
回答3:
I think that the answer by user2660727 is good, because it uses only standard Java, is short and efficient. Correcting a few issues (negative values, buffer length), my suggested solution is:
InetAddress bar = InetAddress.getByName(ip);
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES).order(ByteOrder.BIG_ENDIAN);
buffer.put(new byte[] { 0,0,0,0 });
buffer.put(bar.getAddress());
buffer.position(0);
long address = buffer.getLong();
来源:https://stackoverflow.com/questions/10087800/convert-a-java-net-inetaddress-to-a-long