ASCII转十六进制小工具



今天要做一个性能测试,开发人员直接从日志中截取了一大堆的十六进制报文给我,但是因为是asc码的,没法直接使用,还得先转换成16进制报文才行。



baidu一下,净是c的,以前倒是用c++ builder写过一个,也有c的,但是早就不知道扔到哪里去了,而且要环境,现在深深感觉到vbs还是好用呀,文本编辑器就可以了。写了两个,一个是直接进行文件转换,另一个就转换一个字符串,用得上的同学可以留着,用不上的同学散了吧。


文件转换的代码:


dim strInFile,strOutFile,strLine
Dim dt1, dt2


dt1 = Now


strInFile="mes1.txt"
strOutFile=getNow() & ".dat"
strall=""


Set fs=createobject("scripting.filesystemobject")
Set f=fs.opentextfile(strInFile,1,true)


Const adTypeBinary = 1 
Const adSaveCreateOverWrite = 2 
Dim stream, xmldom, node 
Set xmldom = CreateObject("Microsoft.XMLDOM") 
Set node = xmldom.CreateElement("binary") 
node.DataType = "bin.hex" 


Set stream = CreateObject("ADODB.Stream") 
stream.Type = adTypeBinary 
stream.Open 


Do Until f.AtEndOfStream
  strLine=f.ReadLine
  node.Text = strLine & vbcrlf
stream.write node.NodeTypedValue 
Loop


f.close
Set f=nothing
Set fs=nothing


stream.saveToFile strOutFile, adSaveCreateOverWrite 
stream.Close 
Set stream = Nothing 
Set node = Nothing 
Set xmldom = Nothing 




dt2 = Now
MsgBox "执行完成,总共用时:" & DateDiff("s", dt1, dt2) & "秒"


Function getNow()
Dim d
Dim sRet
d=Now()
sRet=Year(d) & Right("00" & Month(d),2) & Right("00" & Day(d),2) & Right("00" & Hour(d),2) & Right("00" & Minute(d),2) & Right("00" & Second(d),2)
getNow=sRet
End Function



单行转换的代码:

'写二进制文件
Call WriteBinary("001.dat","2A485132303031383932333737333639362C41443126413131343635333232333338333433313134303733333634363030313932313031313426423032303030303030303026433030313A35353E3826463030303023")
MsgBox "done"


Sub WriteBinary(FileName, Buf) 
Const adTypeBinary = 1 
Const adSaveCreateOverWrite = 2

Dim stream, xmldom, node 
Set xmldom = CreateObject("Microsoft.XMLDOM") 
Set node = xmldom.CreateElement("binary") 
node.DataType = "bin.hex" 
node.Text=Buf 
Set stream = CreateObject("ADODB.Stream") 
stream.Type = adTypeBinary 
stream.Open 
stream.write node.NodeTypedValue 
stream.saveToFile FileName, adSaveCreateOverWrite 
stream.Close 
Set stream = Nothing 
Set node = Nothing 
Set xmldom = Nothing 
End Sub 



你可能感兴趣的:(vbs,asc转16进制)