9394952

导航

VBS下将gbk转utf-8

通过zabbix监控windows,由于zabbix server运行在linux下,采用utf-8内码处理,而windows则以gbk(gb2312)做内码,所以当有中文内容上报到zabbix时,会造成编码错误

通过实践,经以下代码处理后,输出基本上符合需求,需不无完全保证无乱码,但已经完全可以接受

msg="需要转码的内容"

instru_msg=""
for i=1 to len(msg) 
	Bin=mid(msg,i,1)
	If RegExpTest("[\u4e00-\u9fa5]", Bin) Then
		if showdebug then
			wscript.echo Bin & "--HZ"
		end if
		instru_msg=instru_msg & conv(Bin)
	else
		instru_msg=instru_msg & Bin
	end if
next
Wscript.Echo instru_msg

Function conv(strIn)
    Set adoStream = CreateObject("ADODB.Stream")
	adoStream.Charset = "utf-8"
    adoStream.Type = 2 'adTypeText 
    adoStream.Open  
    adoStream.WriteText strIn
    adoStream.Position = 0  
    adoStream.Charset = "_autodetect_all"  
    adoStream.Type = 2 'adTypeBinary  
    conv = adoStream.ReadText()  
    adoStream.Close  
      
'	conv = Mid(conv, 1)  
End Function 
Function RegExpTest(patrn, strng)   
    Dim regEx, retVal ' 建立变量。   
    Set regEx = New RegExp ' 建立正则表达式。   
    regEx.Pattern = patrn ' 设置模式。   
    regEx.IgnoreCase = False ' 设置是否区分大小写。   
    retVal = regEx.Test(strng) ' 执行搜索测试。   
    If retVal Then   
        RegExpTest = True  
    Else   
        RegExpTest = False  
    End If   
End Function

 

posted on 2021-01-08 10:27  9394952  阅读(398)  评论(0编辑  收藏  举报