Write hex values to binary file in VBA Excel

Fractalis

Active Member
Joined
Oct 11, 2011
Messages
328
Office Version
  1. 365
Platform
  1. Windows
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.
 
It's done!
I used strings all the way as you suggested and apparently it's working, thanks!
This is how I handled the header:


VBA Code:
 NumSamples = ncaracteres * 6 * wavesperbit * samplesperwave
 subchunk2size = Hex(NumSamples)
 subchunk2size = Right(subchunk2size, 2) & " 0" & Left(subchunk2size, 1) & " 00 00"
 
 
 Header = "52 49 46 46 24 08 00 00 57 41 56 45 66 6d 74 20 10 00 00 00 01 00 01 00 44 ac 00 00 20 62 05 00 02 00 10 00 64 61 74 61 "
 Header = Header & subchunk2size

This chunk was later converted to a byte array to which I appended the rest of the data, after also being converted to a byte array.
 
Upvote 0

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
WELL DONE! (y)
It's usually more satisfying when you finally get it done yourself.
 
Upvote 0

Forum statistics

Threads
1,223,268
Messages
6,171,100
Members
452,379
Latest member
IainTru

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top