Reading/Writing 8-bit files


Author Message
rodent59
Newbie

Joined: 02/03/2012
Location: Australia
Posts: 7
Posted: 12:23pm 06 Mar 2012      

This may well be generally known, but just in case it's not...
For MMBasic3.1

While all INPUT and PRINT commands deal with ASCII (7-bit) characters, they can handle 'whole' 8-bit bytes as follows:

Open any non-text file (say a .pdf or .jpg for example) and read a single byte:
a$=input$(1,#1)
print ASC(a$) 'returns the 8 bit result (0 to 255) or
print HEX$(ASC(a$)) 'returns the hex code for the byte.

When writing to a file:
print #2,CHR$(250); 'will write a single byte to file with decimal code 250 or
print #2,CHR$(&HE8); 'will write a single byte to file with hex code E8.

Might be useful if reading/writing non-text files sequentially.
Apologies if this is old news!




Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3367
Posted: 06:12am 08 Mar 2012      

Good point rodent.

In fact ver 2.1 could not do this and it was only in ver 2.3 (I think) that I rewrote the string handling to fix it. Now all string handling is 8 bit "clean". So you can (for example) do a$ = chr$0) and so on.

Also, USB is 8 bit clean, so you can print a null character to the host PC by doing PRINT chr$(0)

GeoffEdited by Geoffg 2012-03-09
Geoff Graham - http://geoffg.net

PeterR
Newbie

Joined: 28/06/2011
Location:
Posts: 2
Posted: 09:49am 08 Mar 2012      

Thanks Rodent, I been trying to work out how to send 8 bit non text data.

Geoff, can you use this on a serial port?

Eg.
open "com1:4800" as #1
print #1,chr$(0)

I seem to be having issues.

Peter
Peter

Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3367
Posted: 10:35pm 08 Mar 2012      

  PeterR said  Geoff, can you use this on a serial port?

Eg.
open "com1:4800" as #1
print #1,chr$(0)

I seem to be having issues.

Yes, that works fine. I have an application that drives a serial LCD using codes like that.

Geoff
Geoff Graham - http://geoffg.net

aargee
Senior Member

Joined: 21/08/2008
Location: Australia
Posts: 255
Posted: 04:10am 09 Mar 2012      

That's why my cursory go at serial GLCD driving failed! Thanks for the thread everyone.

- Rob
For crying out loud, all I wanted to do was flash this blasted LED.