Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 16:50 09 May 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Binary values in files

Author Message
PilotPirx

Regular Member

Joined: 03/11/2020
Location: Germany
Posts: 94
Posted: 01:00pm 07 Mar 2025
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 3996
Posted: 01:07pm 07 Mar 2025
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 4249
Posted: 01:10pm 07 Mar 2025
Copy link to clipboard 
Print this post

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
Print

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: Germany
Posts: 1526
Posted: 01:11pm 07 Mar 2025
Copy link to clipboard 
Print this post

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: Germany
Posts: 94
Posted: 01:25pm 07 Mar 2025
Copy link to clipboard 
Print this post

That's it. I love this forum! Thank you!
 
William Leue
Guru

Joined: 03/07/2020
Location: United States
Posts: 405
Posted: 02:40pm 07 Mar 2025
Copy link to clipboard 
Print this post

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: Australia
Posts: 549
Posted: 12:10am 08 Mar 2025
Copy link to clipboard 
Print this post

It's a pity MMBasic does not have a 8-bit (byte or char) datatype, AFAIK
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2414
Posted: 12:33am 08 Mar 2025
Copy link to clipboard 
Print this post

I think these
  Quote  MEMORY PACK source%()/sourceaddress%, dest%()/destaddress%, number, size
MEMORY UNPACK source%()/sourceaddress%, dest%()/destaddress%, number, size
or POKE / PEEK are as close as you will get.
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 7499
Posted: 08:02am 08 Mar 2025
Copy link to clipboard 
Print this post

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: Australia
Posts: 549
Posted: 10:14am 08 Mar 2025
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 4249
Posted: 10:27am 08 Mar 2025
Copy link to clipboard 
Print this post

  zeitfest said  Apart from the associated overheads involved I think MMBasic uses 8 bytes/64 bits for each numerical variable.


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: Germany
Posts: 1526
Posted: 12:39pm 08 Mar 2025
Copy link to clipboard 
Print this post

  thwill said  ... Each arrays also allocate a minimum of 256 bytes from the heap. ...
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
Print "Heap: ";MM.Info(Heap)
Print
Memory
End

Regards
Michael
causality ≠ correlation ≠ coincidence
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4249
Posted: 12:50pm 08 Mar 2025
Copy link to clipboard 
Print this post

  twofingers said  Do you mean for every element? I don't think so.


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 Kingdom
Posts: 4249
Posted: 12:56pm 08 Mar 2025
Copy link to clipboard 
Print this post

... 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: Germany
Posts: 1526
Posted: 01:07pm 08 Mar 2025
Copy link to clipboard 
Print this post

@Tom
Thanks for the clarification.
causality ≠ correlation ≠ coincidence
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025