In OSX such a file can be generated with the command: hexdump -ve '1/1 "%.2x"' inputfile > outputfile
For Windows you can use the DOS-program "sfk.exe" which can be downloaded from
https://www.windows10download.com/swiss-file-knife/
See info in the attached file "BIN to HEX procedure" The following code translates the hex-file to the original binary file. The file should contain pure HEX-code without carriage-return or other ASCII characters.
Choose Select=1 or 2 or 3 for testing. The selection section can simply be extended with user-files.
Code: Select all
'HEXtoBIN by Dutchman, September 2017
'Transform hex-file to binary file
'In OSX-terminal the hex-file can be generated with
' hexdump -ve '1/1 "%.2x"' inputfile > outputfile
'In Windows in the DOS-command line window
' with the program sfk.exe
'See file "BIN to HEX procedure" for details
'
'
'==== SELECT OPTIONS ====
Select=1 'Test1, Test2, Test3, User1, User2
ON Select GOTO Test1, Test2, Test3, User1, User2
'
'==== User filenames ====
' File names to select
User1: ' set filenames InFile$ and BinFile$
InFile$=""
BinFile$=""
GOTO Preset
User2: ' set filenames InFile$ and BinFile$
InFile$=""
BinFile$=""
GOTO Preset
'
'==== Test files ====
Test1: ' small file
Infile$="alarm.wavHEX"
BinFile$="alarm.wav"
GOTO Preset
Test2: ' large file
Infile$="hardy.mp3HEX"
BinFile$="Hardy.mp3"
GOTO Preset
Test3: ' wrong file-format
Infile$="alarm.wavHEXdump"
BinFile$="alarm.wav"
GOTO Preset
'
Preset:'==== PRESETS ====
OPTION BASE 1
Steps=40 ' number of progress steps
MinSize=5E5 ' minimum filesize to show progress
'
'==== MAIN ====
'--- test Infile
IF Infile$="" THEN
PRINT "Infile$ is not set" ! STOP
ELSE ! IF NOT FILE_EXISTS(InFile$) THEN
PRINT "InFile$ """&Infile$&""" NOT found"
STOP ! ENDIF
ENDIF
'
'--- Read file and determine size
filesize=FILE_SIZE(InFile$)
PRINT "HEXtoBIN operating on file """&InFile$&""""
PRINT "Filesize=";"#":filesize
DIM BIN(Filesize/2)
TIME RESET
PRINT "Reading file"
FILE InFile$ READLINE line$
Tr=TIME()
linesize=LEN(Line$)
IF Linesize<>Filesize THEN
PRINT "*** ERROR *** Invalid file-format"
STOP
ENDIF
nd=linesize/2
'
'--- set stepsize np
IF nd>=MinSize THEN
np=FLOOR(nd/steps) ' set progress steps
ELSE ! np=nd+1 ! ENDIF
'
'--- Transfer stringdata into numeric array
PRINT "Decoding to bytes"
DIM Bin(nd)
TIME RESET
FOR i=1 TO nd
Bin(i)=DEC(MID$(Line$,2*i-1,2))
IF i%np=0 THEN PRINT CHR$(9632);'show progress
NEXT i
Td=TIME() 'decode time
IF nd>np THEN PRINT 'terminate progress
'
'--- Write to binary file
IF BinFile$="" THEN
PRINT "*** ERROR *** BinFile$ not set"
STOP
ENDIF
IF FILE_EXISTS(BinFile$) THEN FILE BinFile$ DELETE
FILE BinFile$ WRITEDIM Bin
Tw=TIME()-Td ' write time
PRINT
PRINT "#":nd;"bytes written to """&BinFile$&"""."
'
'---- write run-times
PRINT "Run-time:"
F$="#.##"
PRINT " Read: ";f$:Tr;"seconds"
PRINT " Decode: ";f$:Td;"seconds"
PRINT " Write: ";f$:Tw;"seconds"
'
'--- Test binary file
IF Select<4 THEN
TIME RESET
PRINT "Test on binary file """&BinFile$&"""."
MUSIC 1 LOAD BinFile$
MUSIC 1 PLAY
DO ! UNTIL NOT MUSIC_PLAYING("1")
FILE BinFile$ DELETE
PRINT "Time played: ";f$:TIME();"seconds."
ENDIF
PRINT "Done"
END
Remove the extension ".txt" before start of the program. For the longer test with Select=2, the file "hardy.mp3HEX" is required and is attached. Remove the ".txt"-extension before use. In case the file contains carriage -return characters the program will abort.
That is tested with Select=3 with the file "alarm.wavHEXdump" which is generated with hexdump without format parameters in OSX
The HEX-file is attached, remove ".txt" before use. The complete HEX2BIN-folder can be downloaded from https://www.dropbox.com/sh/z9j037m0trn1 ... AfEFa?dl=0
The DOS-program "sfk.exe" is also added to the Dropbox-folder and attached here: Remove the extension ".txt" before use.