0
votes

I would like to import a raw file that is not plaintext. The values I'd like to import is a single line of character that I need to import, split into single characters,and convert to Hex values between 00 and FF. There are 49152 characters and I'd like it to import in 3 columns. A 3 x 16384 like this:

01 02 03
04 05 06

↓this↓ works but doesn't give leading zeros. (1 should be 01). also, It ignores values that do not cover all three columns.↓

Sub Demo() Dim fl As String Dim dat As Variant Dim fn As Integer Dim rOutput As Range Dim idx As Long Dim FileLength As Long Dim v

fn = FreeFile
fl = "C:\Users\1097180551N\Desktop\untitled.png"

Set rOutput = [A1]

Open fl For Binary Access Read As #fn

FileLength = LOF(fn)
ReDim dat(1 To FileLength \ 3, 1 To 3)

For idx = 1 To UBound(dat, 1)
    v = Input(3, #fn)
    dat(idx, 1) = Hex(Asc(Mid$(v, 1, 1)))
    dat(idx, 2) = Hex(Asc(Mid$(v, 2, 1)))
    dat(idx, 3) = Hex(Asc(Mid$(v, 3, 1)))
Next
rOutput.Resize(UBound(dat, 1), 3) = dat
Close #fn
End Sub
1

1 Answers

1
votes

Your last update code is nearly there. I would read the file as Binary rather than Input, and convert the characters into printable form. Also, using a variant array will be faster than writing the the sheet directly

Sub Demo()
    Dim fl As String
    Dim dat As Variant
    Dim fn As Integer
    Dim rOutput As Range
    Dim idx As Long
    Dim FileLength As Long
    Dim v

    fn = FreeFile
    fl = "C:\Users\1097180551N\Desktop\txt.raw"

    Set rOutput = [A1]

    Open fl For Binary Access Read As #fn

    FileLength = LOF(fn)
    ReDim dat(1 To FileLength \ 3, 1 To 3)

    For idx = 1 To UBound(dat, 1)
        v = Input(3, #fn)
        dat(idx, 1) = Hex(Asc(Mid$(v, 1, 1)))
        dat(idx, 2) = Hex(Asc(Mid$(v, 2, 1)))
        dat(idx, 3) = Hex(Asc(Mid$(v, 3, 1)))
    Next
    rOutput.Resize(UBound(dat, 1), 3) = dat

    Close #fn
End Sub