[SNMP4J] UTF Encoding Support

He haobo haobohe at gmail.com
Sun Jan 4 08:15:29 CET 2009


Hi Frank,

I took a look at the toString() method of OctetString that is like below.
  public String toString() {
    if (isPrintable()) {
      return new String(value);
    }
    return toHexString();
  }

  public boolean isPrintable() {
    for (int i=0; i<value.length; i++) {
      char c = (char)value[i];
      if ((Character.isISOControl(c) ||
          ((value[i] & 0xFF) >= 0x80)) && (!Character.isWhitespace(c))) {
        return false;
      }
    }
    return true;
  }

So if the value byte array contains Chinese chars, it will not display the
correct Chinese chars and will display the hexadecimal values. You know, in
order to display the Chinese chars correctly, I have to do some encoding
converting just like this.
new String(((OctetString) vb[i].getVariable()).toByteArray(), "gb2312");
After done that, the chinese chars will be correctly displayed. Therefore, I
guess if the value byte array contains Japanese chars, I will also have to
use a Japanese charset to reconstruct the text. Same process will be needed
for other chars such as German, French and so on.

My problem is that I am writing a snmp trap receive client and I can't know
which chars the snmp trap sender(such as cisco router and some kinds of
firewall) will use by advance. So in this case, I have to guess which
charsets the snmp trap sender are using. Do you have any suggestion for this
case? Thanks.


Eternal




2009/1/2 Frank Fock <fock at agentpp.com>

> Hi,
>
> OctetString does not "use" any encoding. As the name
> suggests, it represents an octet string which is
> a (binary) string of 8-bit bytes.
>
> If you convert an OctetString to a String, UTF-8 is
> used by default (Java does that). You are responsible
> to use the correct format as specified in the
> corresponding MIB specification of the object.
>
> Regards,
> Frank
>
>
> karunakaran s.l wrote:
>
>> Hi All,
>>
>>            I want to know the snmp4j's  "OctetString" uses the UTF
>> encoding
>> or not.
>>
>> If it uses the UTF Encoding means what is encoding type (8 or 16). If not
>> using UTF encoding means is any other way of using UTF Encoding for
>> setting
>> the MO Values of type OctetString. (Example: UTF encoding for
>> sysLocation).
>>
>> Please clarify this as soon as possible.
>>
>>
>> Thanks
>> Karunakaran
>> _______________________________________________
>> SNMP4J mailing list
>> SNMP4J at agentpp.org
>> http://lists.agentpp.org/mailman/listinfo/snmp4j
>>
>
> --
> AGENT++
> http://www.agentpp.com
> http://www.mibexplorer.com
> http://www.mibdesigner.com
>
>
>
> _______________________________________________
> SNMP4J mailing list
> SNMP4J at agentpp.org
> http://lists.agentpp.org/mailman/listinfo/snmp4j
>



More information about the SNMP4J mailing list