|
Unsigned32 specifies a value whose range includes only non-negative integers (0 to 4294967295).
Examples
Unsigned32 -- same as Unsigned32(0..4294967295)
Unsigned32(0..65535) -- corresponds to an unsigned 16 bit integer
Unsigned32(0..10 | 50 | 65 | 90..100)
Create Variable
Using SnmpVar class
The SnmpVar class is the base class for all SNMP variable classes. The createVariable(String value, byte type)
method creates a new SnmpVar object with the specified type and value.
|
String value = "4292967200"; SnmpVar snmpvar = SnmpVar.createVariable(value, SnmpAPI.UNSIGNED32) |
The string value should only be in decimal.
Valid Usage
|
SnmpVar snmpvar = SnmpVar.createVariable("4294967295", SnmpAPI.UNSIGNED32) // 0xffffffff SnmpVar snmpvar = SnmpVar.createVariable("0", SnmpAPI.UNSIGNED32) SnmpVar snmpvar = SnmpVar.createVariable("4294967296", SnmpAPI.UNSIGNED32) // 0x100000000 |
The above usage should be used carefully. The given value will be converted to zero while sending an SNMP request because only the least significant 4 bytes are taken.
Invalid Usage
|
SnmpVar snmpvar = SnmpVar.createVariable("test", SnmpAPI.UNSIGNED32); |
The above usage is invalid because only decimal values are accepted.
|
SnmpVar snmpvar = SnmpVar.createVariable("429496729542949672954294967295", SnmpAPI.UNSIGNED32); |
The above usage is invalid because, the value given exceeds the maximum limit of the "long" value.
|
SnmpVar snmpvar = SnmpVar.createVariable("0x123abc", SnmpAPI.UNSIGNED32); |
The above usage is invalid because only decimal values are accepted and not any hexadecimal values. For all of the above invalid usages, an SnmpException is thrown.
Using SnmpUnsignedInt class
The constructor of this class creates an SnmpUnsignedInt
object by taking a long type as argument.
|
long value = 4292967200; SnmpUnsignedInt snmpunsignedint = new SnmpUnsignedInt(value); |
Using mibs package
The following code snippet creates an SnmpVar object of Unsigned32 data type using the LeafSyntax
class.
|
String mibFile = "<mib name>"; String moduleName = "<module name>"; String oid = "<OID>"; MibOperations mibOps = new MibOperations(); mibOps.loadMibModule(mibFile); MibModule module = mibOps.getMibModule(moduleName); SnmpOID snmpoid = mibOps.getSnmpOID(oid); MibNode node = module.getMibNode(snmpoid); LeafSyntax syntax = node.getSyntax(); String value = "4292967200"; SnmpVar snmpvar = syntax.createVariable(value); |
Valid Usage
|
SnmpVar snmpvar = syntax.createVariable("65535"); SnmpVar snmpvar = syntax.createVariable("'1111111111111111'B"); SnmpVar snmpvar = syntax.createVariable("'FFFF'H"); |
|
|
Note: The value can be given in decimal, hex or binary format while sending an SNMP request because only the least significant 4 bytes are taken. |
Retrieve Value from Variable
Using SnmpVar Class
Following are the methods of SnmpUnsignedInt
class that are used to retrieve value from the created variable.
1. The getVarObject() returns the value as Long object.
|
long value = 0xffffffffL; // the maximum value of an Unsigned32 SnmpUnsignedInt unsigned = new SnmpUnsignedInt(value); Long obj = (Long)unsigned.getVarObject(); |
2. The toValue() method returns the value as Long object.
|
Long obj = (Long )unsigned.toValue(); |
3. The toBytes() method returns the integer as a byte array of length 4.
|
byte[] array = unsigned.toBytes(); |
This byte array will contain the following values.
array[0] = 0xff;
array[1] = 0xff;
array[2] = 0xff;
array[3] = 0xff;
4. The toString() method returns the integer value in decimal as a printable form.
|
String value = unsigned.toString(); |
This string contains the value "4294967295".
5. The toTagString() method returns the integer value in decimal with the tag "UNSIGNED" attached to it.
|
String tagstring = unsigned.toTagString(); |
This string contains the value "UNSIGNED: 4294967295".
6. The longValue() method returns the value as "long"
|
long value = unsigned.longValue(); |
Using mibs package
The toString()
and toByteString()
methods of the MibOperations class are used to retrieve the value of the variable as String.
|
String value = mibOps.toString(snmpvar, snmpoid); |
|
|
Note: In high-level API, these objects do not have separate classes because they are handled internally. |
|