How to Read Binary Files Using BASICs Without BINARY Access
Retired KB Content Disclaimer
Note that "binary file" is a general term for a file containing astream of bytes with no delimiters. A binary file can be written (orread) with an OPEN access of BINARY or RANDOM in BASIC. To make senseof a binary file, you must also know the record definition (or byteboundaries) of all types of variables stored in the file.
To illustrate the above concepts, examples are provided below. Example2a or 2b running in Microsoft QuickBASIC (b) (IEEE binary mathversion) 1.00 for the Apple Macintosh can read IEEE floating-pointnumbers stored in a file that was created with BINARY or RANDOM accessin QuickBASIC for MS-DOS (in Example 1a or 1b) and uploaded to theMacintosh. These products share the same IEEE format, even though theyrun on different computers. (Note: To transfer the file, you needspecial serial communications or uploading software/hardware capableof transferring binary data files between MS-DOS and Macintoshcomputers.)
The BINARY access mode of the OPEN statement is supported in MicrosoftQuickBASIC Versions 4.00, 4.00b, and 4.50 for MS-DOS, Microsoft BASICCompiler Versions 6.00 and 6.00b for MS-DOS and MS OS/2, and MicrosoftBASIC Professional Development System (PDS) Version 7.00 for MS-DOSand MS OS/2. (These product versions also support a new third variableargument on the PUT# and GET# statements to more conveniently writevariables to BINARY or RANDOM access files, without the need for aFIELD statement).
However, the BINARY OPEN access mode (and the third variable argumenton the PUT# and GET# statements) are NOT features in the followingproducts: in Microsoft QuickBASIC Versions 1.00, 1.01, 1.02, 2.00,2.01, and 3.00 for MS-DOS; in Microsoft BASIC Compiler Versions 5.35and 5.36 for MS-DOS; in Microsoft GW-BASIC Versions 3.20, 3.22, 3.23,and earlier; and in Microsoft QuickBASIC Version 1.00 for the AppleMacintosh.
For QuickBASIC (b) for Macintosh, the numeric conversion from a MS-DOSQuickBASIC 4.00, 4.00b, or 4.50 BINARY or RANDOM file is simple withthe CVI, CVL, CVS, and CVD functions. QuickBASIC (d) (decimal mathversion) for Macintosh can use CVI and CVL, but not CVS or CVD, forthis conversion.
Since MS-DOS BASICs earlier than QuickBASIC 4.00, 4.00b, or 4.50 donot support the LONG type and their floating-point format is not IEEE(except for QB87.EXE in QuickBASIC 3.00), the numbers stored asstrings in their BINARY or RANDOM files are unreadable by CVS and CVDin QuickBASIC for the Macintosh. Integers are readable with the CVIfunction. To convert other numeric values (LONG integer, singleprecision, and double precision), you would have to do complicated bitmanipulation based on the IEEE format to parse the string into anumber recognizable in that BASIC version.
For more information concerning the IEEE format, please see Pages131-134 in the "Microsoft Macro Assembler 5.1: Programmer's Guide," orthe "IEEE and Rounding" application note, which can be obtained bycalling Microsoft Technical Support at (206) 454-2030. Moreinformation can also be found in this database by querying on thefollowing words:
Example 1aThe following code outputs a file with strings containing numericvalues. Run this in QuickBASIC 4.00, 4.00b, or 4.50 for MS-DOS, BASICcompiler 6.00 or 6.00b for MS-DOS, or in BASIC PDS 7.00 for MS-DOS(which all support the OPEN FOR BINARY mode and the third PUT#variable argument). Examples 1a and 1b create an identical BINARY.DATfile on disk for a given BASIC.
OPEN "binary.dat" FOR BINARY AS #1 i% = 1 l& = 2 s! = 3.45! d# = 6.789# PUT #1, , i% PUT #1, , l& PUT #1, , s! PUT #1, , d# CLOSE #1
Example 1bThe following code outputs a file with strings containing numericvalues. Run this in QuickBASIC 4.00, 4.00b, or 4.50 for MS-DOS, BASICcompiler 6.00 or 6.00b for MS-DOS, in BASIC PDS 7.00 for MS-DOS, oreven in QuickBASIC (b) for Macintosh. (Examples 1a and 1b create anidentical BINARY.DAT file on disk for a given BASIC.) Example 1b usesthe FIELD statement technique (with RANDOM access mode):
OPEN "binary.dat" AS #1 LEN = 18 FIELD #1, 2 AS i$, 4 AS lo$, 4 AS S$, 8 AS D$ i% = 1 lo& = 2 s! = 3.45! d# = 6.789# LSET i$ = MKI$(i%) LSET lo$ = MKL$(lo&) LSET s$ = MKS$(s!) LSET d$ = MKD$(d#) PUT #1,1 CLOSE #1
Example 2aThe following program, when run in QuickBASIC (b) for Macintosh (orrun in any other BASIC listed above that uses IEEE format and supportsthe LONG integer data type), will read the BINARY.DAT file created byExample 1a or 1b. This program opens the file FOR INPUT (sequentialaccess) and invokes the INPUT$ function:
OPEN "binary.dat" FOR INPUT AS #1 i$ = INPUT$(2, 1) ' Inputs 2 bytes lo$ = INPUT$(4, 1) ' Inputs 4 bytes s$ = INPUT$(4, 1) ' Inputs 4 bytes d$ = INPUT$(8, 1) ' Inputs 8 bytes PRINT CVI(i$), CVL(lo$), CVS(s$), CVD(d$) CLOSE #1
Example 2bThe following program, when run in QuickBASIC (b) for the Macintosh(or run in any other BASIC listed above that uses IEEE format andsupports the LONG integer data type), will also read the file createdby and uploaded from Example 1a or 1b. Example 2b uses the FIELDstatement technique (with RANDOM access mode):
OPEN "binary.dat" AS #1 LEN = 18 FIELD #1, 2 AS i$, 4 AS lo$, 4 AS S$, 8 AS D$ GET#1,1 PRINT CVI(i$), CVL(lo$), CVS(s$), CVD(d$) CLOSE #1
Article ID: 59290 - Last Review: 01/09/2003 03:53:47 - Revision: 1.1