/** * 将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; }
/** * 将SnmpVar转换为int * * @param xVar SnmpVar * @return int * @throws Exception */ public static long snmpVar2Long(SnmpVar var) { long result = 0; if (var instanceof SnmpInt) { SnmpInt intVar = (SnmpInt) var; result = intVar.longValue(); } else if (var instanceof SnmpGauge) { SnmpGauge gauge = (SnmpGauge) var; result = gauge.longValue(); } return result; }
/** * SnmpVar转换为DateTime类型 * * @param var * @return */ public static String snmpVar2DateAndTime(SnmpVar var) { // 添加对于系统运行时间是int型的处理,一般是非标准设备比如C2000 if (var instanceof SnmpGauge) { SnmpGauge gauge = (SnmpGauge) var; return convertLong2DateTime(gauge.longValue()); } byte[] bytes = var.toBytes(); int i = bytes.length; byte abyte0[] = new byte[i * 2]; int j = 0; for (int k = 0; k < bytes.length; k++) { abyte0[j] = (byte) (bytes[k] >> 4 & 15); abyte0[++j] = (byte) (bytes[k] & 15); j++; } i = abyte0.length; if (i != 16 && i != 22) { return null; } String s = ""; j = abyte0[0] * 4096 + abyte0[1] * 256 + abyte0[2] * 16 + abyte0[3]; s = s + j + "-"; int k = abyte0[4] * 16 + abyte0[5]; s = s + k + "-"; int l = abyte0[6] * 16 + abyte0[7]; s = s + l + ","; int i1 = abyte0[8] * 16 + abyte0[9]; s = s + i1 + ":"; int j1 = abyte0[10] * 16 + abyte0[11]; s = s + j1 + ":"; int k1 = abyte0[12] * 16 + abyte0[13]; s = s + k1 + "."; int l1 = abyte0[14] * 16 + abyte0[15]; s = s + l1; if (i > 16 && i != 32) { s = s + ","; char c = (char) (abyte0[16] * 16 + abyte0[17]); s = s + c; int i2 = abyte0[18] * 16 + abyte0[19]; s = s + i2 + ":"; int j2 = abyte0[20] * 16 + abyte0[21]; s = s + j2; } return s; }
/** * 将SnmpVar转换为时间,排除+8时区的问题 * * @param var * @return */ public static String snmpVar2Timeticks_String_TimeZone(SnmpVar var) { String resu = ""; if (var == null) { return resu; } if (var instanceof SnmpTimeticks) { SnmpTimeticks loSnmp = (SnmpTimeticks) var; resu = loSnmp.toString(); } if (var instanceof SnmpGauge) { SnmpGauge timeVar = (SnmpGauge) var; resu = DateFormatter.getLongDate((timeVar.longValue() - 8 * 3600) * 1000); } else if (var instanceof SnmpInt) { SnmpInt timeVar = (SnmpInt) var; resu = DateFormatter.getLongDate((timeVar.longValue() - 8 * 3600) * 1000); } return resu; }
/** * 将SnmpVar转换为时间 * * @param xVar SnmpVar * @return * @throws Exception */ public static Long snmpVar2Timeticks_Long(SnmpVar var) { // 改写方法返回String Long resu = 0L; if (var == null) { return resu; } if (var instanceof SnmpTimeticks) { SnmpTimeticks loSnmp = (SnmpTimeticks) var; resu = loSnmp.longValue(); } if (var instanceof SnmpGauge) { SnmpGauge timeVar = (SnmpGauge) var; resu = timeVar.longValue(); } else if (var instanceof SnmpInt) { SnmpInt timeVar = (SnmpInt) var; resu = timeVar.longValue(); } return resu; }
/** * 将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; }