Write hex values to binary file in VBA Excel

Fractalis

Active Member
Joined
Oct 11, 2011
Messages
252
Hello to all,


I want to write the hexadecimal values to a binary file in order they look the same when I open in hex editor.

My current code is this:

Rich (BB code):
Sub Write2Binary()
Dim i As Integer
Dim nFileNum As Integer
Dim sFilename As String


sFilename = "D:\OutputPath\Test.bin"


strBytes = "F3 A1 02 00 04 00 8D 24 44 C3 8C 03 83 49 26 92 B5"
arrBytes = Split(strBytes)


nFileNum = FreeFile


Open sFilename For Binary Lock Read Write As #nFileNum 


  For i = LBound(arrBytes) To UBound(arrBytes)
    ' No byte position is specified so writing begins at byte 1
    Put #nFileNum , , arrBytes(i)
  Next i


Close #nFileNum 
    
End Sub
This code produces the following binary file that when I open it in a Hex editor looks like this:
Rich (BB code):
08 00 02 00 46 33 08 00 02 00 41 31 08 00 02 00
30 32 08 00 02 00 30 30 08 00 02 00 30 34 08 00 
02 00 30 30 08 00 02 00 38 44 08 00 02 00 32 34 
08 00 02 00 34 34 08 00 02 00 43 33 08 00 02 00 
38 43 08 00 02 00 30 33 08 00 02 00 38 33 08 00 
02 00 34 39 08 00 02 00 32 36 08 00 02 00 39 32 
08 00 02 00 42 35

That is different to the content I want to have in binary file. When I open the file in Hex editor I like to see the following content:

Rich (BB code):
F3 A1 02 00 04 00 8D 24 44 C3 8C 03 83 49 26 92 B5
How can I do this?

Thaks for any help.
 

joeu2004

Well-known Member
Joined
Mar 2, 2014
Messages
2,586
Office Version
2010
Platform
Windows
There are many ways to skin this cat. The devil is in details that you do not provide and I don't have the time to test. Hopefully, the following demonstration will help you invent a solution that is tailored to your goals. The basic idea is: represent hex numeric values by prefixing with "&h".

Code:
Sub testit()
Dim strBytes As String, s As String
Dim h As Byte, b As Variant
strBytes = "F3 A1 02 00 04 00 8D 24 44 C3 8C 03 83 49 26 92 B5"
For Each b In Split(strBytes)
    h = "&h" & b
    s = s & " " & Hex(h)
Next
MsgBox Mid(s, 2)
End Sub
 

Fractalis

Active Member
Joined
Oct 11, 2011
Messages
252
There are many ways to skin this cat. The devil is in details that you do not provide and I don't have the time to test. Hopefully, the following demonstration will help you invent a solution that is tailored to your goals. The basic idea is: represent hex numeric values by prefixing with "&h".

Code:
Sub testit()
Dim strBytes As String, s As String
Dim h As Byte, b As Variant
strBytes = "F3 A1 02 00 04 00 8D 24 44 C3 8C 03 83 49 26 92 B5"
For Each b In Split(strBytes)
    h = "&h" & b
    s = s & " " & Hex(h)
Next
MsgBox Mid(s, 2)
End Sub
No, doesn't work.

Thanks for the help.
 

shg

MrExcel MVP
Joined
May 7, 2008
Messages
21,719
Office Version
2010
Platform
Windows
Code:
Sub Write2Binary()
  Dim asInp()       As String
  Dim ai()          As Byte
  Dim i             As Long
  Dim iFF           As Integer
  Const sFile       As String = "C:\Users\shg\Desktop\Test.bin"

  asInp = Split("F3 A1 02 00 04 00 8D 24 44 C3 8C 03 83 49 26 92 B5")
  ReDim ai(0 To UBound(asInp))

  For i = 0 To UBound(asInp)
    ai(i) = "&H" & asInp(i)
  Next i

  iFF = FreeFile
  Open sFile For Binary Lock Read Write As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=iFF]#iFF[/URL] 

  Put [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=iFF]#iFF[/URL] , , ai
  Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=iFF]#iFF[/URL] 
End Sub
 

joeu2004

Well-known Member
Joined
Mar 2, 2014
Messages
2,586
Office Version
2010
Platform
Windows
No, doesn't work
I suspect you took my demonstration too literally. I did not expect to have to provide a turnkey solution.

The point was to show how to convert "F3" into a numeric hex value, namely &hF3.

The following code demonstrates how to write to your file, based on your example.

Rich (BB code):
Sub Write2Binary()
Dim b As Variant
Dim nFileNum As Integer
Dim sFilename As String

strBytes = "F3 A1 02 00 04 00 8D 24 44 C3 8C 03 83 49 26 92 B5"

sFilename = "D:\OutputPath\Test.bin"
nFileNum = FreeFile
Open sFilename For Binary Lock Read Write As #nFileNum 

  For Each b In Split(strBytes)
    ' No byte position is specified so writing begins at byte 1
    Put #nFileNum , , CByte("&h" & b)
  Next

Close #nFileNum   
End Sub
Doing a byte-by-byte Put is probably not the most efficient way to do this. I'm just following your lead.

PS.... I see that shg demonstrated the more efficient redesign.
 
Last edited:

Fractalis

Active Member
Joined
Oct 11, 2011
Messages
252
Code:
Sub Write2Binary()
  Dim asInp()       As String
  Dim ai()          As Byte
  Dim i             As Long
  Dim iFF           As Integer
  Const sFile       As String = "C:\Users\shg\Desktop\Test.bin"

  asInp = Split("F3 A1 02 00 04 00 8D 24 44 C3 8C 03 83 49 26 92 B5")
  ReDim ai(0 To UBound(asInp))

  For i = 0 To UBound(asInp)
    ai(i) = "&H" & asInp(i)
  Next i

  iFF = FreeFile
  Open sFile For Binary Lock Read Write As [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=iFF"]#iFF[/URL] 

  Put [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=iFF"]#iFF[/URL] , , ai
  Close [URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=iFF"]#iFF[/URL] 
End Sub
Thanks shg, it works pretty nice.
 

Fractalis

Active Member
Joined
Oct 11, 2011
Messages
252
I suspect you took my demonstration too literally. I did not expect to have to provide a turnkey solution.

The point was to show how to convert "F3" into a numeric hex value, namely &hF3.
I tested your suggestion but I missed the CByte() part to make it work. Thanks for share another solution.
 

shg

MrExcel MVP
Joined
May 7, 2008
Messages
21,719
Office Version
2010
Platform
Windows
You're welcome.
 

Forum statistics

Threads
1,082,478
Messages
5,365,783
Members
400,850
Latest member
Raj_Jpr

Some videos you may like

This Week's Hot Topics

Top