java 网络字节转换器,网络字节顺序到主机字节顺序的转换Java

I am using a windows-dll through jna for communicating with some dongle servers. The dll provides three functions, which are connect, disconnect and getting the connection status of the dongles. Connecting and disconnecting works. But when I query the status, the device should return an ip-address which i save into a DWORD. The code is something like, where DWORDByReference IpAddress is an out-parameter:

int AwUsbGetConnectionStatus(String Hub, DWORDByReference IpAddress, IntByReference Status, DWORD Timeout, HANDLE Event);

DWORD value = ipAddress.getValue();

The specification of the dll states: "IpAddress is in host byte order and can be converted to TCP/IP network byte order using the WinSock htonl function."

In another thread i read that the java equivalent to htonl is:

static int htonl(int val) {

return ByteBuffer.allocate(4).putInt(val)

.order(ByteOrder.nativeOrder()).getInt(0);

}

But the problem is, that i get weird values. But somehow they do correspond to the supposed value of the ip-addresses.

For example when the original Ip was: 192.168.102.118

it returns 1986439360

and when i convert it with the method above, it's -1062705546.

But i guess it should be 192168102118 when converted properly

Also another Ip-Address i tried was:

192.168.102.112

for which i get 1885776064

and when i convert that it's -106270552

So the difference between the Ip-Addresses is still 6 for the original Address and for the converted value. I tried this with different Ips and the difference between the Ips always matches the supposed values.

So does anyone know, if there is something wrong with the way i convert the value the dll-function returns or could there even be a problem when i get the value of the DWORD parameter?

解决方案

You should set the order before putting the little-endian address into it.

And it appears (from your comment "i guess it should be 192168102118 when converted properly") that you want dotted-quad output; for that, using a byte array may be more natural:

static byte[] htoip(int ipv4) {

return ByteBuffer.wrap(new byte[4])

.order(ByteOrder.nativeOrder())

.putInt(ipv4)

.array();

}

This result is suitable for passing to InetAddress.getByAddress(), etc.

While this isn't the simplest way to reverse 4 bytes, its use of ByteOrder.nativeOrder() makes it portable and future-proof.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值