Page 1 of 2

HEXdump2BIN for OSX

Posted: Tue Sep 05, 2017 6:08 pm
by Dutchman
Anticipating a new SP-version without Dropbox-acces, I developed a method to transfer binary files (e.g. soundfiles) from OSX to SB.
OSX has a command to make a hexadecimal dump of a file to a textfile.
That textfile can be transferred to SB via copy and paste and then recoded into a binary file.
I made an example with the soundfile "alarm.wav"
In the terminal-app in OSX that file is transferred into the hexdump file "alarm.wavHEXdump" with the command:
hexdump -C -v alarm.wav > alarm.wavHEXdump
More details are given in the attached file "HEXdump in OSX procedure"
HEXdump in OSX procedure.txt
(1.22 KiB) Downloaded 342 times
The following picture gives a screenshot of the terminal-window.
Terminal hexdump screen.png
Terminal hexdump screen.png (147.92 KiB) Viewed 3663 times
With the following code the file "alarm.wavHEXdump" is then recoded to the binary soundfile and tested.

Code: Select all

'HEXdump2BIN by Dutchman, September 2017
'Transform hexdump-file from OSX to binary file
'
'==== USER OPTIONS ====
Infile$="alarm.wavHEXdump"
HexFile$="alarm.wavHEX"
BinFile$="alarm.wav"
'
'==== CONSTANTS ====
'
'==== PRESETS ====
OPTION BASE 1
'
'==== MAIN ====
'--- determine sizes
filesize=FILE_SIZE(InFile$)
FILE InFile$ READLINE line$
linesize=LEN(Line$)
PRINT "Operating on file """&InFile$&""":"
PRINT "Filesize=";"#":filesize
PRINT "Line length=";"#":linesize
lines=CEIL(filesize/linesize)
ns=lines*16 ' size of string-array
PRINT "Number of lines=";"#":lines
PRINT "Number of bytes in binary file is <=";"#":ns
DIM Dump$(ns)
'
'--- Read data into array
nd=0 'number of binary data
WHILE NOT FILE_END(InFile$)
  'cut final part
  i=INSTR(line$,"|",1)
  Line$=LEFT$(Line$,i-1)
  SPLIT Line$ TO m$,n WITH " "
  FOR i=1 TO n-1 ! Dump$(nd+i)=m$(i+1) ! NEXT i
  nd+=n-1 ' exclude address-data
  FILE InFile$ READLINE line$  
END WHILE
PRINT nd;"hex codes read."
PRINT "Last line contained ";"#":nd%16;"codes."
DIM Bin(nd)
'
'--- Write to binary file
IF FILE_EXISTS(BinFile$) THEN FILE BinFile$ DELETE
FOR i=1 TO nd
  Bin(i)=DEC(Dump$(i))
NEXT i
FILE BinFile$ WRITEDIM Bin
PRINT "Binary data written to """&BinFile$&"""."
'
'--- Test binary file
IF BinFile$="alarm.wav" THEN
  PRINT "Test on binary file """&BinFile$&"""."
  MUSIC 1 LOAD "alarm.wav"
  FOR i=1 TO 3
    MUSIC 1 PLAY
    PAUSE 1
  NEXT i
  FILE BinFile$ DELETE
ENDIF
PRINT "Done"
END

The hexdump-file "alarm.wavHEXdump" is also attached.
alarm.wavHEXdump.txt
(82.16 KiB) Downloaded 310 times
Remove the extension ".txt" before start of the the program
The complete folder "HEX2BIN" can also be downloaded from https://www.dropbox.com/sh/z9j037m0trn1 ... AfEFa?dl=0

Re: HEX2BIN for OSX

Posted: Tue Sep 05, 2017 7:14 pm
by rbytes
A great solution! Thanks, Ton. :D

I assume there will also be some Windows users on the Forum who would like to be able to hexdump binary files for Smart Basic. I work with both Mac and PC. So I did a little searching this afternoon and found a simple program that can do this and much more. It is called Swiss File Knife and it is available at http://windows10download.com

When you get to their site, use their search window to find the file.

If you have an older version of Windows, just search for "Windows hex dump" and you will find multiple solutions.

Re: HEX2BIN for OSX

Posted: Tue Sep 05, 2017 8:22 pm
by rbytes
I used Swiss File Knife on my Windows 10 tablet to do the hex dump of an MP3 file.

It crashed the HEXtoBIN program with an error. I have attached the screenshots. Perhaps the file is too large? It is around 2.5 MB, I believe. I don't think variable i should be -1.

Re: HEX2BIN for OSX

Posted: Tue Sep 05, 2017 8:37 pm
by Dutchman
Please show the result, a few lines, of the HEXdump-file. Has it the same format as in the screenshot of my first post in this thread?
The command LEFT$ is used to remove the part starting with "|" on each line.
The correct format is (See manual for 'hexdump'):
Display the input offset in hexadecimal, followed by sixteen space-separated, two column, hexadecimal bytes, followed by the same sixteen bytes in %_p format enclosed in "|'' characters.
According to the value of Line$ in your screenshot, the format of your hexdump is quite different, it is a line which contains only 16 hexadecimal codes.

Re: HEX2BIN for OSX

Posted: Tue Sep 05, 2017 8:51 pm
by rbytes
Yes, my hexdump doesn't have the "|" characters.

Swiss File Knife has many parameters that can be used with their hexdump routine. I will have to see if your required format is available. One thing I did notice is that with the two parameter variations I tried, I got very large files. From my 2.1 MB .mp3 file, the dump I tested was 4.5 MB and the other (untested as yet) was 8.5 MB! Could be that the larger file does have the right format, since that would explain why it is so big. I will test it tonight.

Re: HEX2BIN for OSX

Posted: Tue Sep 05, 2017 8:58 pm
by Mr. Kibernetik
In ancient times when attachments in emails were in text format, binary files were encoded as ASCII in BASE64 format: https://en.wikipedia.org/wiki/Base64

Re: HEX2BIN for OSX

Posted: Tue Sep 05, 2017 9:07 pm
by Dutchman
For a pure hex dump, the file size will be two times larger than the original size. I used the -C (capital C) parameter because that gives unicode text. The parameter '-c' (lower-case c) gave an error at opening in texteditor: "textcoding is not in 'Unicode (UTF-8)'" (translated from Dutch)
The generated hexdump file is now about five times the original size :!:

Re: HEX2BIN for OSX

Posted: Tue Sep 05, 2017 9:12 pm
by Dutchman
Mr. Kibernetik wrote:
Tue Sep 05, 2017 8:58 pm
In ancient times when attachments in emails were in text format, binary files were encoded as ASCII in BASE64 format: https://en.wikipedia.org/wiki/Base64
I was looking for a solution without the need for programming on my desktop computer. And it was a piece of cake :D

Re: HEX2BIN for OSX

Posted: Tue Sep 05, 2017 9:19 pm
by Dutchman
rbytes wrote:
Tue Sep 05, 2017 8:51 pm
Yes, my hexdump doesn't have the "|" characters.

Swiss File Knife has many parameters that can be used with their hexdump routine. I will have to see if your required format is available. One thing I did notice is that with the two parameter variations I tried, I got very large files. From my 2.1 MB .mp3 file, the dump I tested was 4.5 MB and the other (untested as yet) was 8.5 MB! Could be that the larger file does have the right format, since that would explain why it is so big. I will test it tonight.
In your place, I would use the pure hex dump. It is only two times the original size and it needs only the DEC()-command. No SPLIT and LEFT$.
I propose two programs: "HEX2OSXbin.sb" and "HEX2WINbin.sb" :D

Re: HEX2BIN for OSX

Posted: Tue Sep 05, 2017 9:36 pm
by rbytes
Not sure I understand. I tried modifying a copy of HEX2BIN and commenting out the lines with SPLIT and LEFT$. Then tried converting my file to binary, Still got an error. The program is trying to dim BIN to a negative number.