Reading+and+writing+nonstandard+values

//by J.G.Harston, July 2009// Read a 16-bit integer code format="bb4w" DEF FNrd16(I%):=BGET#I%+256*BGET#I% code Read a 32-bit integer (same as **INPUT#I%,A%** but as a function, see notes) code format="bb4w" DEF FNrd32(I%):LOCAL A%:DIM zp% LOCAL 3:FOR A%=0 TO 3:zp%?A%=BGET#I%:NEXT:=!A% code Read a length-specified string (string length followed by string of up to 255 characters) code format="bb4w" DEF FNrdStrN(I%):LOCAL A%,A$:A%=BGET#I%:IFA%=0:="" REPEATA$=A$+BGET#I%:A%=A%-1:UNTILA%<1:=A$ code Read a length-specified reversed string (string length followed by reversed string of up to 255 characters) code format="bb4w" DEF FNrdStrR(I%):LOCAL A%,A$:A%=BGET#I%:IFA%=0:="" REPEATA$=BGET#I%+A$:A%=A%-1:UNTILA%<1:=A$ code Read a bit7-terminated string (final character has bit 7 set) code format="bb4w" DEF FNrdStrB7(I%):LOCAL A%,A$:REPEAT:A%=BGET#I%:A$=A$+CHR$(A%AND127):UNTILA%>127:=A$ code Write a 16-bit integer code format="bb4w" DEF PROCwr16(O%,A%):DIM zp% LOCAL 3:!zp%=A%:FOR A%=0 TO 1:BPUT#O%,zp%?A%:NEXT:ENDPROC code Write a 32-bit integer (same as **PRINT#O%,A%**, see notes) code format="bb4w" DEF PROCwr32(O%,A%):DIM zp% LOCAL 3:!zp%=A%:FOR A%=0 TO 3:BPUT#O%,zp%?A%:NEXT:ENDPROC code Write a length-specified string (string length followed by string of up to 255 characters) code format="bb4w" DEF PROCwrStrN(O%,A$):LOCAL A%:BPUT#O%,LENA$:IFA$="":ENDPROC FOR A%=1 TO LEN A$:BPUT#O%,ASCMID$(A$,A%,1):NEXT:ENDPROC code Write a length-specified reversed string (string length followed by reversed string of up to 255 characters) code format="bb4w" DEF PROCwrStrN(O%,A$):LOCAL A%:BPUT#O%,LENA$:IFA$="":ENDPROC FOR A%=LEN A$ TO 1 STEP -1:BPUT#O%,ASCMID$(A$,A%,1):NEXT:ENDPROC code Write a bit7-terminated string (final character has bit 7 set) code format="bb4w" DEF PROCwrStrB7(O%,A$):BPUT#O%,LEFT$(A$,LENA$-1);:BPUT#O%,ASCRIGHT$(A$,1)OR128:ENDPROC code

Comments by Richard Russell
I don't believe that **INPUT#I%,A%** reads four bytes, nor that **PRINT#O%,A%** writes four bytes, in any version of BBC BASIC. In //BBC BASIC for Windows// they read/write **five** bytes (in ***FLOAT40** mode) or **eight** bytes (in ***FLOAT64** mode).

code format="bb4w" DEF FNrd32(I%)=BGET#I%+(BGET#I%<<8)+(BGET#I%<<16)+(BGET#I%<<24) code The 'length-specified' string routines appear to assume a maximum string length of 255 characters, but this is not stated.
 * FNrd32** is more straightforwardly implemented as follows (it should also be considerably faster than the code listed above):

In several instances the routines listed could be simplified at the expense of compatibility with old versions of BBC BASIC (e.g. 6502 and Z80), for example by using the **+=** and **–=** operators.