Bin2Hex example
Posted: Sun Jan 30, 2005 12:11 pm
Code updated For 5.20+
Hi all!
Here is a procedure to convert a binary file to a hex file direct.
It is smart to use when you want to convert big binary files.
It isen't so fast, around 2750kB/s with 2048 buffersize without prewrite, on my pc. So if you have a faster example that works, post it here.
Hi all!
Here is a procedure to convert a binary file to a hex file direct.
It is smart to use when you want to convert big binary files.
It isen't so fast, around 2750kB/s with 2048 buffersize without prewrite, on my pc. So if you have a faster example that works, post it here.
Code: Select all
Procedure Bin2Hex(inputfile.s, outputfile.s)
#buffersize=1024
#prewrite=#False
If FileSize(inputfile)<>-1
filelength=FileSize(inputfile)
If CreateFile(1, outputfile)
If #prewrite=#True
*buffermemory=AllocateMemory(filelength*2)
WriteData(1,*buffermemory, filelength*2)
FreeMemory(*buffermemory)
FileSeek(1,0)
EndIf
If OpenFile(2, inputfile)
*tempmemory=AllocateMemory(#buffersize)
*hexmemory=AllocateMemory(#buffersize*2+1)
byteread.w
For x=0 To filelength Step #buffersize
*memstraddr=*hexmemory
byteread=ReadData(2,*tempmemory, #buffersize)
For y=0 To byteread-1
CopyMemoryString(RSet(Hex(PeekB(*tempmemory+y)&$FF), 2, "0"), @*memstraddr)
Next y
WriteString(1,PeekS(*hexmemory,(byteread*2)+1))
Next x
FreeMemory(*hexmemory)
FreeMemory(*tempmemory)
CloseFile(1)
CloseFile(2)
ProcedureReturn 1
Else
ProcedureReturn -2
EndIf
Else
ProcedureReturn -1
EndIf
Else
ProcedureReturn 0
EndIf
EndProcedure
input.s=OpenFileRequester("Bin file:", "", "", 0)
output.s=SaveFileRequester("Hex file:", "", "", 0)
thesize=FileSize(input)
first=ElapsedMilliseconds()
result.b=Bin2Hex(input, output)
second=ElapsedMilliseconds()
kbs.f=(thesize/1024)/((second-first)/1000)
theoutputline.s=theoutputline+"Result: "+Str(result)
theoutputline=theoutputline+#CRLF$+"The time: "+Str(second-first)+" milliseconds"
theoutputline=theoutputline+#CRLF$+"The size of file: "+Str(thesize)+" bytes"
theoutputline=theoutputline+#CRLF$+"Input file: "+input
theoutputline=theoutputline+#CRLF$+"Output file: "+output
theoutputline=theoutputline+#CRLF$+"kB/s: "+StrF(kbs, 0)
theoutputline=theoutputline+#CRLF$+"Buffersize: "+Str(#buffersize)+" bytes"
theoutputline=theoutputline+#CRLF$+"Prewrite: "+Str(#prewrite)
MessageRequester("Results:", theoutputline)