(This post is updated on September 27th heavily.)
If you notice, both SNMP data type
OCTET STRING and
Opaque can be viewed as byte array type in .NET. Maybe you wonder why
OCTET STRING is not viewed as
System.String, the reason is simple.
System.String does have one more property than a byte array,
Encoding. It seems when SNMP was designed, it does not provide a default encoding for
OCTET STRING explicitly (and sometimes it is even used just like a byte array when you store MAC address bytes in it).
Therefore, a problem has been there for a long time. Which encoding should be used there if an
OctetString instance comes. In the old Change Sets, the answer is ASCII and there is no way in them to change it to Unicode or others.
It is in the latest few builds, I start to provide a way so that you can explicitly state which encoding you prefer.
As a reference at first, there are new methods and properties in
OctetString class you should pay attention to,
public OctetString(string str, Encoding encoding). You may provide a specific
Encodingso that the internal bytes are what you expect.
public string ToString(Encoding encoding). According to
Encodingprovided, the internal bytes are decoded to a correct string.
public Encoding Encoding. This is a read only property that illustrates what encoding is assigned to this
public static Encoding DefaultEncoding. This is a property to define default encoding used for all
OctetStringinstance if no encoding is assigned via the constructor described in item 1. The default value of this property is
public OctetString(string str).
OctetString.DefaultEncodingis used to generate internal bytes from
public string ToString().
OctetString.Encodingis used to generate a string from internal bytes.
Now let’s review a few common scenarios and discuss what you may do to customize
If you don’t touch the properties and methods related to
Encoding, then you are in a pure ASCII way. It should behave the same as old releases of #SNMP (0.5, 1.0 and 1.1).
If you change
DefaultEncoding to any other
Encoding types, and don’t touch the properties and methods related to
Encoding later, you are in a pure
If some of your devices require ASCII while others require Unicode (aka UTF-16), then you may find it a little bit difficult. My suggestions are,
Whenever you need to construct an
OctetString object, specify its encoding explicitly. Whenever you need to use an
OctetString object, check its
Encoding before calling
ToString(). If the
Encoding is not correct, call
I understand the current implementation is not perfect yet. When #SNMP parses incoming packets, it always use
DefaultEncoding to construct
OctetString objects from raw bytes. This is not optimal for hybrid cases. Hope a better way can be found soon.
No matter what, please report any critical issues or your suggestions in this field.