[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 399
  • Last Modified:

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.
0
kfc23
Asked:
kfc23
  • 4
1 Solution
 
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
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now