Can I use a colon length to judge if the result is OctetString or String format?

I use snmp4j to query some data .At first,I use colon length to judge whether the result is OctetString or String format.Fox example,it the length>6,than it is OctetString .But I find this way is wrong. Can anyone tell me how to udge whether the result is OctetString or String format ?

 OctetString octetString=new OctetString("who:am");
        System.out.println(octetString.toHexString()); //OctetString  with 6 conlons
        String testString="uT:DCN:SD-QD-HD-W-12.CN2.10GE0/1/0/2::CTVPN35036G::0505-ODF-07.A9/10"; 
        System.out.println(testString.split(":").length); //normal string with 7 colons 

Trying to determine what is contained in an OctetString is essentially guessing, unless you reference the MIB. (And you can’t assume that any OctetString will contain any colons at all… Remember it is just a “string of bytes,” not necessarily “bytes of a text string.”) That said, SNMP4J’s implementation of OctetString has a handy method that works for some general display purposes: isPrintable(). If true, then you could call toASCII() to get a text interpretation of the bytes, rather than the colon-delimited hex from toHexString(). Still, there are no guarantees you will get anything useful unless you reference the MIB and know exactly what is supposed to be in that mysterious string of bytes. Even then, sometimes an agent will put different things in a varbind in different situations. So it is always tricky. Write your code defensively to be safe.

1 Like