/** * 将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转换为byte[] * * @param xVar SnmpVar * @return byte[] * @throws Exception */ public static byte[] snmpVar2ByteArray(SnmpVar xVar) { if (xVar == null) { return new byte[0]; } byte[] array = new byte[0]; if (xVar instanceof SnmpString) { SnmpString strVar = (SnmpString) xVar; array = strVar.toBytes(); } return array; }