Creating a BMP from Hex data

Hello,

I need to write a quick app to create a BMP from a stream of Hex digits. The BMP header information is present. Any ideas on how to do this. Do i need to convert the hex to binary and then output the data to a  newly created .bmp file ?

Thanks,

Ken.
kfc23Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

fds_fatboyCommented:
Try this. Create a new standard vb application. Drop this code onto Form1, use the filepath of an existing bmp where I have used "...coffee bean.bmp" and run.

The bits of code you will be interested in are Hex2Dec and WriteFile.

Option Explicit

Private Function GetHex() As String
    Dim bt As String
    Dim i As Long
    Dim s As String
    Dim ch As Long

    ch = FreeFile
   
    Open "C:\WINNT\coffee bean.bmp" For Binary Access Read As #ch
    bt = String$(LOF(ch), vbNull)
    Get #ch, , bt
    Close #ch

    For i = 1 To Len(bt)
        s = s & Right$("00" & Hex$(Asc(Mid$(bt, i, 1))), 2)
    Next
    GetHex = s
End Function

Private Function Hex2Dec(HexString As String) As Long
    Const numchars = "0123456789ABCDEF"
    Dim i As Long
    Dim r As Long
   
    For i = 1 To Len(HexString)
        r = r * 16
        r = r + InStr(1, numchars, Mid$(HexString, i, 1)) - 1
    Next
   
    Hex2Dec = r
End Function

Public Sub WriteFile(HexInfo As String, FilePath As String)
    Dim s As String
    Dim i As Long
    Dim ch As Long
   
    For i = 1 To Len(HexInfo) Step 2
        s = s & Chr$(Hex2Dec(Mid$(HexInfo, i, 2)))
    Next

    ch = FreeFile
    Open FilePath For Binary Access Write As #ch
    Put #ch, , s
    Close #ch
End Sub

Private Sub Form_Load()
    Dim HexInfo As String
    HexInfo = GetHex()
   
    WriteFile HexInfo, "c:\temp\test.bmp"
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fds_fatboyCommented:
In my haste, I've just realised that Hex2Dec is a misnomer and it would be better named something like Hex2Long. But the code works.
0
fds_fatboyCommented:
Does it do what you want?
0
fds_fatboyCommented:
My solution will work. I think I understood the question.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.