[snmp] TrapPduv1.getIpAddress() returns negative values
Birgit Arkesteijn
birgit at westhawk.co.uk
Tue Nov 7 09:52:35 GMT 2006
Hi Jordi,
I realise I missed out the subject line yesterday and only read the
message body. Yes, Josh hit the nail on the head.
For printing the IP address, why don't you use the JDK, instead of doing
it yourself? By using
java.net.InetAddress.getByAddress(byte[] addr).getHostName()
you'll make sure the code works for IPv4 and IPv6.
Cheers, Birgit
On Tue, Nov 07, 2006 at 12:19:47AM +0100, Jordi Vea i Barbany wrote:
> You are right. The stack wasn't the problem. The thing is that you
> can't cast byte to int so easily.
>
> To represent it as a String I was doing:
>
> byte[] addr = trap.getIpAddress();
> String agentAddr = addr[0] + "." + addr[1] + "." + addr[2] + "." +
> addr[3];
>
> Doing the following solves the problem:
>
> byte[] addr = trap.getIpAddress();
> String agentAddr = ( addr[0] & 0xFF ) + "." + ( addr[1] & 0xFF ) +
> "." + ( addr[2] & 0xFF ) + "." + ( addr[3] & 0xFF ) ;
>
> Thanks a lot.
>
> Jordi Vea
--
-- Birgit Arkesteijn, birgit at westhawk.co.uk,
-- Westhawk Ltd, Albion Wharf, 19 Albion Street, Manchester M1 5LN, UK
-- tel.: +44 (0)161 237 0660
-- <URL: http://www.westhawk.co.uk>
More information about the snmp
mailing list