Code archives/File Utilities/Convert any file to bb data file
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
| I wrote this just for fun really, and also to get some practise in as I learn the Blitzbasic language. Further details are in the code, you will need blitzclose.zip in the same folder if you want to run the code exactly as it is, otherwise just change the filename. | |||||
;Convert any file to bb data file, on 28/10/2005
;
;what it does: this will read any file's binary data and convert it
;to the Blitzbasic data stack format. The resulting .bb file can be
;used, for example, to "pack" image files into a Blitzbasic program
;or, to store a file as code without the need for web hosting space.
;The example used the blitzclose.zip (see ../User Input/Blitz Close)
file$ = "blitzclose.zip" ;<- specify a file to convert to .bb data
datafile$ = "blitzclose.bb" ;<- specify the .bb file to create
filein = ReadFile(file$) ;Read file
size = FileSize (file$)
Dim array(size)
For s=1 To size ;read bytes to array
byte = ReadByte(filein)
array(s) = byte
Next
CloseFile(filein) ;End read file
; ;Paste this commented code at the start of the .bb file
; ;This code will just recreate the file from the data stack
;Restore startData ;init data pointer
;fileout = WriteFile("blitzclose.zip") ;<- specify the file to recreate
;While byte>=0
; Read byte ;get next byte
; WriteByte(fileout,byte)
;Wend
;CloseFile(fileout)
;WaitKey()
fileout = WriteFile(datafile$) ;Write file
temp$=";paste code here" ;comment
For s=1 To Len(temp$)
char$=Mid$(temp$,s,1) : WriteByte(fileout,Asc(char$))
Next
WriteByte(fileout,13) : WriteByte(fileout,10) ;carr return & linefeed
temp$=".startData" ;first "Data " line
For s=1 To Len(temp$)
char$=Mid$(temp$,s,1) : WriteByte(fileout,Asc(char$))
Next
WriteByte(fileout,13) : WriteByte(fileout,10) ;carr return & linefeed
temp$="Data " ;first "Data " line
For s=1 To Len(temp$)
char$=Mid$(temp$,s,1) : WriteByte(fileout,Asc(char$))
Next
For i=1 To size ;write bytes from array to ascii data
If count>16 ;new line
count=0
WriteByte(fileout,13) : WriteByte(fileout,10) ;carr return & linefeed
temp$="Data " ;next "Data " line
For s=1 To Len(temp$)
char$=Mid$(temp$,s,1) : WriteByte(fileout,Asc(char$))
Next
EndIf
If array(i)<10 ;write single figures
WriteByte(fileout,array(i)+48) ;units (ie. write ascii "0-9")
count=count+1 ;(ie. comma if not last on line and not last byte)
If count<=16 And i<>size Then WriteByte(fileout,Asc(",")) ;comma
EndIf
If array(i)>=10 And array(i)<100 ;write double figures
num=array(i) : tens=0
While num>=10 : num=num-10 : tens=tens+1 : Wend ;get tens & units
WriteByte(fileout,tens+48) ;tens
WriteByte(fileout,num+48) ;units
count=count+1
If count<=16 And i<>size Then WriteByte(fileout,Asc(",")) ;comma
EndIf
If array(i)>=100 And array(i)<256 ;write treble figures
num=array(i) : tens=0 : cents=0
While num>=100 : num=num-100 : cents=cents+1 : Wend ;get cents
While num>=10 : num=num-10 : tens=tens+1 : Wend ;get tens & units
WriteByte(fileout,cents+48) ;cents
WriteByte(fileout,tens+48) ;tens
WriteByte(fileout,num+48) ;units
count=count+1
If count<=16 And i<>size Then WriteByte(fileout,Asc(",")) ;comma
EndIf
Next
CloseFile(fileout) ;End write file
Print "file in="+file$ ;tell us its done
Print "file size="+size
Print "file out="+datafile$
WaitKey() |
Comments
| ||
| A nice amendment would be : file$ = RequestFile ("Choose a file to pack") datafile$ = Left (file$, Len(file$)-3)+"bb" ; this is assuming you have selected a file with a 3 letter extension You could go further and specify a directory to save all data files. It a pity that files over 200k will take and age to load in BLitzPlus, on a Core Duo 1.86, i hand it running for 8 minutes trying to load the converted bb file. |
| ||
| What else can this be useful for, besides tiny graphics and sounds? |
| ||
| Not much really, it allows you to post files as code on the forum instead of having to host them somewhere which is fine as long as the files aren't too many/big. |
Code Archives Forum