Home > Back-end >  Java snmp4J window information Chinese garbled
Java snmp4J window information Chinese garbled

Time:11-30

Get Windows of information through SNMP, meet Chinese as hexadecimal, conversion or not, specific information is as follows:
Oid for Windows system time: 1.3.6.1.2.1.25.1.2.0=07: e3:09:0 b: 15:06:14:06
As above information through the following methods after the conversion is gibberish, including specified code: utf-8, GB2312, GBK, ISO - 8859-1 conversion is garbled,

Several attempts fail, the great god and see how to convert,

Public static String getChinese (String octetString) {//snmp4j in Chinese directly into hexadecimal String
Try {
String [] temps=octetString. Split (" : ");
Byte [] bs=new byte [temps. Length];
for (int i=0; i Bs [I]=(byte) Integer. ParseInt (temps [I], 16);

//GB2312 ISO - 8859-1 ASCII
Return a new String (bs, "GB2312");
} the catch (Exception e) {
e.printStackTrace();
return null;
}
}

/* *
* directly into hexadecimal string (need not Unicode decoding)
* @ param hexStr
* @ return
* @ throws UnsupportedEncodingException
*/
Public static String hexStr2Str (String hexStr) throws UnsupportedEncodingException {
String STR="0123456789 abcdef";
Char [] hexs=hexStr. ToCharArray ();
Byte [] bytes=new byte [hexStr length ()/2];
int n;
for (int i=0; i N=STR. IndexOf (hexs (2 * I]) * 16;
N +=STR. IndexOf (hexs (2 * I + 1));
Bytes [I]=(byte) (n & amp; 0 XFF);
}
Return a new String (bytes);
}


/* *
* hexadecimal conversion become type string string
* @ param s
* @ return
*/
Public static String hexStringToString (String s) {
If (s==null | | s.e quals (" ")) {
return null;
}
S=s.r eplace (" ", "");
Byte [] baKeyword=new byte [s.l ength ()/2];
for (int i=0; i Try {
BaKeyword [I]=(byte) (0 XFF & amp; Integer. ParseInt (s.s ubstring (I * 2, I * 2 + 2), 16));
} the catch (Exception e) {
e.printStackTrace();
}
}
Try {
S=new String (baKeyword, "utf-8");
New String ();
{} the catch (Exception e1)
E1. PrintStackTrace ();
}
return s;
}

CodePudding user response:

Looking forward to the great god to save, for many days, Chinese garbled question not to save.

CodePudding user response:

Public static String getFormatStr (String octetString) {
Try {
if(! OctetString. The contains (" : ")) {
Return the octetString;
}
String [] temps=octetString. Split (" : ");
Byte [] bs=new byte [temps. Length];
for(int i=0; iBs [I]=(byte) Integer. ParseInt (temps [I], 16);

String encodename=null;
Encodename=EncodeUtils. GetEncode (new BufferedInputStream (new ByteArrayInputStream (bs)), true);
{if (StrUtil. IsEmpty (encodename))
Encodename="utf-8";
}
Return a new String (bs, encodename);
} the catch (Exception e) {
return null;
}
}

CodePudding user response:

Reason: the problem on the basis of SNMP4J string class OctetString,


https://blog.csdn.net/lele892207980/article/details/10587647

Another person's code:

https://my.oschina.net/chengpengvb/blog/3105482
  • Related