将hex文件转换为Excel文件

我正在使用汉纳传感器仪器来logging一些数据,这些数据是以.hex代码的.log文件forms出现的。 然后我使用Hanna软件(与仪器一起提供)将这些数据转换为excel格式(数字和字符以及特殊字符)。 我想知道它是如何做到的,如果可能的话,自己做没有软件?

该文件看起来像这样,其扩展名是.log

4d65 7465 720a 3230 3137 3039 3139 0000 c1fb 5321 ffff 01fc 1fc8 ffff f4ff f92d 0dc3 58ff 4a00 0000 0000 735a 0081 6101 a242 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 003d 3950 82cc 0940 08b4 3088 c2fb 5321 ffff 01fc 1fc8 ffff f4ff 0b2e 0dae 58ff 4a00 0000 0000 a85a 00a8 6101 1143 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 . . . continued 

尝试像这样:

 Sub JustOneFile() Dim s As String, i As Long i = 1 Close #1 Open "C:\TestFolder\james.log" For Input As #1 Do Until EOF(1) Line Input #1, s Cells(i, 1) = s i = i + 1 Loop Close #1 Columns("A:A").TextToColumns Destination:=Range("B1"), DataType:=xlFixedWidth, _ FieldInfo:=Array(Array(0, 2), Array(4, 2), Array(9, 2), Array(14, 2), Array(19, 2), _ Array(24, 2), Array(29, 2), Array(34, 2)), TrailingMinusNumbers:=True End Sub 

在这里输入图像说明

该过程涉及到导入.LOG文件,然后循环遍历所有单元格并单独转换它们或将所有数据块存储到变体数组中,将其转换并将转换后的值返回到工作表。 后一种转换方法是推荐的。

 Option Explicit Sub wqewqw() Dim hxwb As Workbook Dim i As Long, j As Long, vals As Variant Workbooks.OpenText Filename:="%USERPROFILE%\Documents\hex_2_excel.log", _ Origin:=437, StartRow:=1, DataType:=xlFixedWidth, TrailingMinusNumbers:=True, _ FieldInfo:=Array(Array(0, 2), Array(4, 2), Array(9, 2), Array(14, 2), _ Array(19, 2), Array(24, 2), Array(29, 2), Array(34, 2)) With ActiveWorkbook With .Worksheets(1) vals = .Cells(1, "A").CurrentRegion.Cells For i = LBound(vals, 1) To UBound(vals, 1) For j = LBound(vals, 2) To UBound(vals, 2) vals(i, j) = Application.Hex2Dec(vals(i, j)) Next j Next i With .Cells(1, "A").Resize(UBound(vals, 1), UBound(vals, 2)) .NumberFormat = "General" .Value = vals End With End With End With End Sub 

在这里输入图像说明