Copy binary file into array in basic

Anything QL Software or Programming Related.
User avatar
t0nyt
Gold Card
Posts: 385
Joined: Wed Nov 22, 2023 6:46 pm
Location: UK

Copy binary file into array in basic

Post by t0nyt »

Hi All,

It's been 30 odd years since I did any real programming on a QL and 5 years since I did ANY kind of programming

I was wondering if someone could point me in the direction, using standard QL basic with no toolkits, of how to read a 4kb BINARY file into an array please? or into memory that I could peek I guess

Currently I've used inkey$ to read each byte, but I'm concerned some bytes may end up wrong as I assume inkey$ is ascii biased? Also, inkey$ method is very slow and this will have to run with every debug

Many thanks


User avatar
Pr0f
QL Wafer Drive
Posts: 1308
Joined: Thu Oct 12, 2017 9:54 am

Re: Copy binary file into array in basic

Post by Pr0f »

Are you creating a flat 1 x 4096 character array? So each byte of your 4K file represents an element in the array?

I thought inkey$ on the QL could handle non ascii characters? My question would be - can it handle all 256 possible values you might expect?


https://buildmedia.readthedocs.org/medi ... f#page=471
Last edited by Pr0f on Fri Jan 05, 2024 12:27 pm, edited 1 time in total.


User avatar
t0nyt
Gold Card
Posts: 385
Joined: Wed Nov 22, 2023 6:46 pm
Location: UK

Re: Copy binary file into array in basic

Post by t0nyt »

Am reading into an array A$(1,4096)

So a$(1,1) = byte 1, a$(1,2) = byte 2 etc

Yes re inkey$, I did mean I doubt it handles all 256 bytes correctly. Didn’t express myself very well

Thanks
Last edited by t0nyt on Fri Jan 05, 2024 12:28 pm, edited 1 time in total.


User avatar
Pr0f
QL Wafer Drive
Posts: 1308
Joined: Thu Oct 12, 2017 9:54 am

Re: Copy binary file into array in basic

Post by Pr0f »

Just amended my post with the link to the online manual - you may be in luck with inkey$ :-)


User avatar
dilwyn
Mr QL
Posts: 2761
Joined: Wed Dec 01, 2010 10:39 pm

Re: Copy binary file into array in basic

Post by dilwyn »

t0nyt wrote: Fri Jan 05, 2024 11:55 am Hi All,

It's been 30 odd years since I did any real programming on a QL and 5 years since I did ANY kind of programming

I was wondering if someone could point me in the direction, using standard QL basic with no toolkits, of how to read a 4kb BINARY file into an array please? or into memory that I could peek I guess

Currently I've used inkey$ to read each byte, but I'm concerned some bytes may end up wrong as I assume inkey$ is ascii biased? Also, inkey$ method is very slow and this will have to run with every debug

Many thanks
As far as memory goes, without any toolkits you are limited to reserving memory with RESPR, memory which can't later be released (unlike, say, ALCHP and RECHP in Toolkit 2)

base=respr(4096)
LBYTES filename,base
DIM array$(4095) : REM one less using 0 subscript
FOR a=0 TO 4095
array$(a)=PEEK(base+a)
END FOR a

This isn't too bad in terms of wasted memory if your program keeps the data in memory and uses it repeatedly, since you can't release memory reserved with RESPR. A lot more practical if you use ALCHP (to reserve memory) and RECHP (to release it later).

Otherwise, as you said, I can only think of the slow but sure way of using INKEY$(#channel) to read it all back in without resorting to extensions.

It might have been faster to have saved the data block in 128 byte blocks with PRINT, which INPUT could read back in later (INPUTting blocks can be faster than byte reads). But this means that (a) there are linefeeds at the end of each 128 byte block, and (b) runs the risk that there is a 'stray' linefeed/CHR$ 10 as part of the data which would confuse the INPUT later.

There are small toolkits out there which have ALCHP/RECHP type extensions without the need for the full Toolkit 2, e.g. https://dilwyn.qlforum.co.uk/tk/alchp.zip


User avatar
t0nyt
Gold Card
Posts: 385
Joined: Wed Nov 22, 2023 6:46 pm
Location: UK

Re: Copy binary file into array in basic

Post by t0nyt »

Thanks Dilwyn

LBYTES, of course! I've used that in all the boot files I've been editing, didn't occur to me to use it for this!

I don't need to release the memory so this is perfect

I can just access the contents with peek, no need to transfer it to an array

Many thanks


User avatar
t0nyt
Gold Card
Posts: 385
Joined: Wed Nov 22, 2023 6:46 pm
Location: UK

Re: Copy binary file into array in basic

Post by t0nyt »

Pr0f wrote: Fri Jan 05, 2024 12:28 pm Just amended my post with the link to the online manual - you may be in luck with inkey$ :-)
Thanks Pr0f, have bookmarked it for future use

Thanks


User avatar
t0nyt
Gold Card
Posts: 385
Joined: Wed Nov 22, 2023 6:46 pm
Location: UK

Re: Copy binary file into array in basic

Post by t0nyt »

Is "Assembly Language Programming on the Sinclair QL by Andrew Penell" a good place to start to get back into assembly programming on the QL please?

I have the GST QL Macro assembler manual as well, but have not programmed in assembler in 20 years (and then it was mainly 8086)

Many thanks


User avatar
Artificer
Over Heated PSU
Posts: 129
Joined: Fri Nov 24, 2017 8:43 am

Re: Copy binary file into array in basic

Post by Artificer »

t0nyt wrote: Fri Jan 05, 2024 3:00 pm Is "Assembly Language Programming on the Sinclair QL by Andrew Penell" a good place to start to get back into assembly programming on the QL please?
Hi, I would say so as someone who mainly programs in basic and only uses assembler when forced to. I use the book as a reference when I have to. Another book that I think is useful is the QL Avanced User Guide by Adian Dickens if you can get hold of a copy.

Cheers


User avatar
t0nyt
Gold Card
Posts: 385
Joined: Wed Nov 22, 2023 6:46 pm
Location: UK

Re: Copy binary file into array in basic

Post by t0nyt »

Artificer wrote: Fri Jan 05, 2024 4:43 pm
t0nyt wrote: Fri Jan 05, 2024 3:00 pm Is "Assembly Language Programming on the Sinclair QL by Andrew Penell" a good place to start to get back into assembly programming on the QL please?
Hi, I would say so as someone who mainly programs in basic and only uses assembler when forced to. I use the book as a reference when I have to. Another book that I think is useful is the QL Avanced User Guide by Adian Dickens if you can get hold of a copy.

Cheers
Thanks, I do have a copy of the Dickens book on my shelf

Many thanks


Post Reply