Some OctetString values are not received as expected, why?

Last modified by Frank Fock on 2024/05/25 20:39

First of all, the values are received correctly! The problem seems to occur because of an undefined non-printable-escape-character and the resulting OctetString.toString() formatting. 
To always get the byte values, use OctetString.getValue() or OctetString.toByteArray() methods only.

The following code snippet from a SNMP4J unit test illustrates how OctetString.toString() formats non-printable values:

OctetString Formatting

String HEX_STRING = "1C:32:41:00:4E:38";
OctetString nonPrintable = OctetString.fromHexString(HEX_STRING);
assertFalse(nonPrintable.isPrintable());
assertEquals(HEX_STRING.toLowerCase(), nonPrintable.toString());
SNMP4JSettings.setDefaultNonPrintableEscapeCharacter('_');
assertEquals("_2A_N8", nonPrintable.toString());
SNMP4JSettings.setDefaultNonPrintableEscapeCharacter(null);
assertEquals(HEX_STRING.toLowerCase(), nonPrintable.toString());