示例#1
0
  /**
   * 将SnmpVar转换为字符串
   *
   * @param xVar SnmpVar
   * @return 字符串
   * @throws Exception
   */
  public static String snmpVar2Str(SnmpVar var) {
    String result = null;

    if (var == null) {
      return "";
    }

    // 增加中文支持
    if (var instanceof SnmpString) {
      String str = new String(snmpVar2ByteArray(var));
      if (str.getBytes().length != str.length()) {
        result = str;
      } else {
        SnmpString strSnmp = (SnmpString) var;
        result = (strSnmp.toString());
      }
    } else if (var instanceof SnmpInt) {
      SnmpInt intSnmp = (SnmpInt) var;
      result = "" + intSnmp.intValue();
    } else if (var instanceof SnmpGauge) {
      SnmpGauge snmpGauge = (SnmpGauge) var;
      result = "" + snmpGauge.longValue();
    }

    return result;
  }
示例#2
0
 /**
  * 将SnmpVar转换为int
  *
  * @param xVar SnmpVar
  * @return int
  * @throws Exception
  */
 public static int snmpVar2Int(SnmpVar var) {
   int result = -1;
   if (var instanceof SnmpInt) {
     SnmpInt intVar = (SnmpInt) var;
     result = intVar.intValue();
   } else if (var instanceof SnmpCounter) {
     SnmpCounter counter = (SnmpCounter) var;
     result = (int) counter.longValue();
   } else if (var instanceof SnmpGauge) {
     SnmpGauge gauge = (SnmpGauge) var;
     result = (int) gauge.longValue();
   } else if (var instanceof SnmpUnsignedInt) {
     SnmpUnsignedInt unsignedInt = (SnmpUnsignedInt) var;
     result = (int) unsignedInt.longValue();
   } else if (var instanceof SnmpNull) {
     result = -1;
   } else if (var instanceof SnmpCounter64) {
     SnmpCounter64 counter = (SnmpCounter64) var;
     result = Integer.parseInt(counter.getNumericValueAsString());
   }
   return result;
 }