Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 01:35 04 May 2024 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 : worms (and binary datastored in DATA statements)

Author Message
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 5912
Posted: 09:30pm 23 Jan 2021
Copy link to clipboard 
Print this post

A completely useless waste of time but you may enjoy it (briefly)

programs.zip

As well as the 'worms.bas' program there is a short utility to convert PNG images (or any other binary data) into DATA statements.
This allowed me to included the 5 image file used in 'worms' into the one file. This makes keeping things together easier at the expense of a larger file.

The utility:
 OPTION EXPLICIT
 OPTION DEFAULT NONE
 DIM INTEGER p
 DIM outputLine$, k$
 DIM HEXFile$ = "imageHEX.bas" ' name of the final BAS or INC file with DATA statements
 OPEN  HEXfile$ FOR OUTPUT AS #2
 doheader
 
 FOR p = 1 TO 5
   convert2HEX "TR_worm0"+STR$(p)+".png" , p
 NEXT p
 CLOSE #2
 PRINT "DONE!"
 PAUSE 2000
 CLS
 ' test the results
 EXECUTE "RUN "+CHR$(34)+"imageHEX.bas"+CHR$(34)
 
END
 
SUB convert2HEX imageFile$, p AS INTEGER
 ' convert supplied filename into DATA statements
 ' each group of DATA statements start with a numbered Label: and filename based on 'P'
 LOCAL INTEGER fs,n,m
 LOCAL k$
 fs = MM.INFO(FILESIZE imagefile$)
 IF fs > 0 THEN
   fs = INT(fs/32) + 1
   OPEN  imageFile$ FOR INPUT AS #1
   PRINT #2, "IMAGE"+STR$(p)+":"
   PRINT #2, "DATA "+CHR$(34)+"IMAGE"+STR$(p)+".png"+CHR$(34)
   FOR n = 1 TO fs
     k$ = INPUT$(32,#1)
     FOR m = 1 TO LEN(k$)
       outputLine$ = outputLine$ + HEX$(ASC(MID$(k$,m,1)),2)
     NEXT m
     PRINT #2, "DATA "+CHR$(34)+outputLine$+CHR$(34)
     outputLine$ = ""
   NEXT n
   PRINT #2, "DATA "+CHR$(34)+"***END***"+CHR$(34)
   CLOSE #1
   PRINT #2, ""
 ENDIF
END SUB
 
SUB doheader
 ' header is a sample way to read the DATA and to test the first image file
 ' you would eventually paste the DATA statements into your own program
 PRINT #2, "OPTION EXPLICIT"
 PRINT #2, "OPTION DEFAULT NONE"
 PRINT #2, "dim integer n, fs, m"
 PRINT #2, "dim outputLine$, imageFile$, k$"
 PRINT #2, "restore IMAGE1"
 PRINT #2, "read imageFile$"
 PRINT #2, "open imageFile$ for output as #1"
 PRINT #2, "do"
 PRINT #2, "read k$"
 PRINT #2, "if k$ = "+CHR$(34)+"***END***"+CHR$(34)+" or K$ = "+CHR$(34)+CHR$(34)+" then exit do"
 PRINT #2, "for m = 1 to len(k$) step 2"
 PRINT #2, "n = val("+CHR$(34)+"&h"+CHR$(34)+"+mid$(k$,m,2))"
 PRINT #2, "print #1, chr$(n);"
 PRINT #2, "next m"
 
 PRINT #2, "loop"
 PRINT #2, "close #1"
 PRINT #2, "LOAD PNG "+CHR$(34)+"image1.png"+CHR$(34)
 PRINT #2,"'"
END SUB


Jim
VK7JH
MMedit   MMBasic Help
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 3848
Posted: 10:44pm 23 Jan 2021
Copy link to clipboard 
Print this post

Hi Jim,

Probably not a problem here (and I may be telling you something you already know), but if size becomes an issue then consider using a standard Base64 encoding instead, it's a much more efficient way of storing binary data as simple characters.

YMMV,

Tom
Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 5912
Posted: 11:05pm 23 Jan 2021
Copy link to clipboard 
Print this post

  thwill said  Hi Jim,

Probably not a problem here (and I may be telling you something you already know), but if size becomes an issue then consider using a standard Base64 encoding instead, it's a much more efficient way of storing binary data as simple characters.

YMMV,

Tom

I agree,
I used to use a system called 7plus for sending binary over 'packet radio' which is strictly limited to printable ASCII channels.
Coding it for MMBasic is on the to-do list.

For this case, the extra work was not considered worth it.

Jim
VK7JH
MMedit   MMBasic Help
 
MustardMan

Senior Member

Joined: 30/08/2019
Location: Australia
Posts: 175
Posted: 07:11am 27 Jan 2021
Copy link to clipboard 
Print this post

Ahhh, the days of UUencode and UUdecode...
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 5735
Posted: 09:15am 28 Jan 2021
Copy link to clipboard 
Print this post

Wow! The days of Packet Radio too... :) I've not played with that for many, many years.

I used to pack binary data into strings on the TRS-80 sometimes. It was about as efficient as you could get on there, if rather slow.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
Print this page


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

© JAQ Software 2024