![]() |
Forum Index : Microcontroller and PC projects : Binary values in files
Author | Message | ||||
PilotPirx![]() Regular Member ![]() Joined: 03/11/2020 Location: GermanyPosts: 94 |
Hello, a question concerning MMBasic: I want to read 8-bit binary values from a file. However, the examples in the manuals always refer to ASCII characters. How do I sequentially read a binary value from a non-text file? |
||||
JohnS Guru ![]() Joined: 18/11/2011 Location: United KingdomPosts: 3996 |
You can input anything, I think. Try it! You'll have to use the function INPUT$() I expect. John |
||||
thwill![]() Guru ![]() Joined: 16/09/2019 Location: United KingdomPosts: 4249 |
Naively: ' read-binary.bas Dim s$ Open Mm.CmdLine$ For Input As #1 Do While Not Eof(#1) s$ = Input$(1, #1) Print Hex$(Asc(s$), 2) + " "; Loop And run with: RUN "read-binary.bas", "file-to-read.bin" Best wishes, Tom Edited 2025-03-07 23:10 by thwill MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures |
||||
twofingers Guru ![]() Joined: 02/06/2014 Location: GermanyPosts: 1526 |
Hi, ASCII characters are 8-bit binary values. For example Open fileName$ For Input As #1 For x = 0 To pageSize buffer$(x) = Input$(1,#1) Next x Close #1 Regards MIchael causality ≠ correlation ≠ coincidence |
||||
PilotPirx![]() Regular Member ![]() Joined: 03/11/2020 Location: GermanyPosts: 94 |
That's it. I love this forum! Thank you! |
||||
William Leue Guru ![]() Joined: 03/07/2020 Location: United StatesPosts: 405 |
You can also use the SAVE DATA and LOAD DATA commands to save and restore variable values, including arrays. This is fast. -Bill |
||||
zeitfest Guru ![]() Joined: 31/07/2019 Location: AustraliaPosts: 549 |
It's a pity MMBasic does not have a 8-bit (byte or char) datatype, AFAIK |
||||
phil99![]() Guru ![]() Joined: 11/02/2018 Location: AustraliaPosts: 2414 |
I think these or POKE / PEEK are as close as you will get. |
||||
Mixtel90![]() Guru ![]() Joined: 05/10/2019 Location: United KingdomPosts: 7499 |
A Byte variable would probably take a lot more CPU power than it saves. The chip registers are 32 bits. If you want anything less you need CPU cycles and workspace to pack and unpack them. Of the two, memory is cheaper than CPU cycles if you want a fast system. You can easily use a string as an array of 255 bytes by using POKE VAR and PEEK VAR with your index added to VARPTR for the string. No such trick for individual bits though, but at least you can store 255 values in 256 bytes of ram. Speed isn't great. Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
zeitfest Guru ![]() Joined: 31/07/2019 Location: AustraliaPosts: 549 |
Apart from the associated overheads involved I think MMBasic uses 8 bytes/64 bits for each numerical variable. |
||||
thwill![]() Guru ![]() Joined: 16/09/2019 Location: United KingdomPosts: 4249 |
Each variable takes ~64 bytes because of the variable descriptor. Strings > 7 chars and arrays also allocate a minimum of 256 bytes from the heap. I believe that the variable storage and heap are still independent which is why the MEMORY command reports them separately. And if anyone wonders why I'm sometimes vague on the internals it's because (as well as my bad memory) the PicoMite and MMB4L share a grandparent rather than a parent so there is an increasing divergence in their internals. Best wishes, Tom Edited 2025-03-08 20:34 by thwill MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures |
||||
twofingers Guru ![]() Joined: 02/06/2014 Location: GermanyPosts: 1526 |
Do you mean for every element? I don't think so. I wouldn't say it like that. Dim string test$(90000) length 1 For i = 0 To 90000 test$(i)=Right$(Str$(i),1) Print @(0)i,test$(i); Next Print "Heap: ";MM.Info(Heap) Memory End Regards Michael causality ≠ correlation ≠ coincidence |
||||
thwill![]() Guru ![]() Joined: 16/09/2019 Location: United KingdomPosts: 4249 |
I didn't say it was for every element, I said an array takes a minimum of 256 bytes of heap. If the array requires more storage it is allocated in additional 256 byte pages. A native BYTE type would't save any memory, but would provide convenience over PEEK and POKE. Best wishes, Tom MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures |
||||
thwill![]() Guru ![]() Joined: 16/09/2019 Location: United KingdomPosts: 4249 |
... If you wanted to be pedantic arrays of 8 BYTEs or less could be implemented so they don't require the additional 256 bytes of an array. Tom MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures |
||||
twofingers Guru ![]() Joined: 02/06/2014 Location: GermanyPosts: 1526 |
@Tom Thanks for the clarification. ![]() causality ≠ correlation ≠ coincidence |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |