Convert Hex file to excel file

Viewed 3467

I am using a Hanna sensor instrument to record some data which is coming as .log files having hex code. Then I use Hanna software (came with the instrument) which converts this data to excel format (numbers & character & special characters). I want to know how it does it, and if possible do it myself without the software?

The file looks like this, and its extension is .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
2 Answers

Try something like:

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

enter image description here

The process involves importing the .LOG file and then either looping through all of the cells and converting them individually or storing all of the data block into a variant array, converting them and returning the converted values to the worksheet. The latter method of conversion is recommended.

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

enter image description here

Related