HEXdump2BIN for OSX

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

HEXdump2BIN for OSX

Post 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 3667 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
Last edited by Dutchman on Thu Sep 07, 2017 8:40 am, edited 1 time in total.

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: HEX2BIN for OSX

Post 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.
Last edited by rbytes on Tue Sep 05, 2017 8:27 pm, edited 1 time in total.
The only thing that gets me down is gravity...

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: HEX2BIN for OSX

Post 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.
Attachments
ironman.mp3HEXdump.txt
(4.25 MiB) Downloaded 331 times
IMG_2858.PNG
IMG_2858.PNG (3.36 MiB) Viewed 3660 times
IMG_2859.PNG
IMG_2859.PNG (4.1 MiB) Viewed 3660 times
The only thing that gets me down is gravity...

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: HEX2BIN for OSX

Post 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.
Last edited by Dutchman on Tue Sep 05, 2017 8:57 pm, edited 1 time in total.

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: HEX2BIN for OSX

Post 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.
The only thing that gets me down is gravity...

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: HEX2BIN for OSX

Post 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

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: HEX2BIN for OSX

Post 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 :!:
Last edited by Dutchman on Tue Sep 05, 2017 9:13 pm, edited 1 time in total.

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: HEX2BIN for OSX

Post 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

User avatar
Dutchman
Posts: 851
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: HEX2BIN for OSX

Post 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

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: HEX2BIN for OSX

Post 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.
Attachments
IMG_2861.PNG
IMG_2861.PNG (3.36 MiB) Viewed 3633 times
IMG_2862.PNG
IMG_2862.PNG (4.1 MiB) Viewed 3633 times
IMG_2860.PNG
IMG_2860.PNG (244.21 KiB) Viewed 3635 times
The only thing that gets me down is gravity...

Post Reply