首页 新闻 会员 周边

SNMP读取系统服务列表,其中的中文如何转换?

0
悬赏园豆:50 [已关闭问题] 关闭于 2017-02-20 14:34

oid: .1.3.6.1.4.1.77.1.2.3.1.1  walk

可以返回系统(win)当前运行的服务列表

 

但是中文服务名读出来是:57 65 62 20 E9 83 A8 E7 BD B2 E4 BB A3 E7 90 86 E6 9C 8D E5 8A A1

 

求问如何转回中文?

sun8134的主页 sun8134 | 初学一级 | 园豆:98
提问于:2017-02-19 23:24
< >
分享
所有回答(1)
0

捣鼓明白这是 UTF8中文 转 16进制

 

再给转回来就行了

 

找了个通用转换方法:

 1         ///<summary> 
 2         /// 从16进制转换成汉字 
 3         /// </summary> 
 4         /// <param name="hex"></param> 
 5         /// <param name="charset">编码,如"utf-8","gb2312"</param> 
 6         /// <returns></returns> 
 7         public static string UnHex(string hex, string charset)
 8         {
 9             if (hex == null)
10                 throw new ArgumentNullException("hex");
11             hex = hex.Replace(",", "");
12             hex = hex.Replace("\n", "");
13             hex = hex.Replace("\\", "");
14             hex = hex.Replace(" ", "");
15             if (hex.Length % 2 != 0)
16             {
17                 hex += "20";//空格 
18             }
19             // 需要将 hex 转换成 byte 数组。 
20             byte[] bytes = new byte[hex.Length / 2];
21             for (int i = 0; i < bytes.Length; i++)
22             {
23                 try
24                 {
25                     // 每两个字符是一个 byte。 
26                     bytes[i] = byte.Parse(hex.Substring(i * 2, 2),
27                     System.Globalization.NumberStyles.HexNumber);
28                 }
29                 catch
30                 {
31                     // Rethrow an exception with custom message. 
32                     //throw new ArgumentException("hex is not a valid hex number!", "hex");
33                     return "hex is not a valid hex number!";
34                 }
35             }
36             System.Text.Encoding chs = System.Text.Encoding.GetEncoding(charset);
37             return chs.GetString(bytes);
38         }

 

sun8134 | 园豆:98 (初学一级) | 2017-02-20 14:34
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册