String Datatypes
There are many ways that strings are commonly stored in a binary file. The BinaryFile object supports four different string formats. The null-terminated and fixed-width null-terminated types, as used by C/C++, are supported. Additionally, fixed-width and the length-prefixed string, as used by Pascal, are also supported.
Null-terminated strings are commonly used with C/C++ and other languages. In this format, the characters of the string are stored one by one, with an ending zero character. This allows strings to be of any length. Strings stored in this format can contain any character, except for the zero character. Two types of null-terminated strings are supported.
The readZeroString and writeZeroString methods are used to read and write null-terminated strings. This is an unlimited length string that ends with a null(character 0). The readZeroString accepts no parameters and returns a String object. The writeZeroString accepts a String object to be written.
The readFixedZeroString and writeFixedZeroString methods are used to read and write fixed-length null-terminated strings. This is the type of string most commonly used by the C/C++ programming language. The amount of memory held by this sort of string is fixed, but the length of this string can vary from zero up to one minus the amount of memory reserved for this string. In C/C++ this type of string is written as:
char str[80];
This means that the str variable occupies eighty bytes, but its length can vary from zero to seventy-nine. No matter how long this string is, it is always stored to a disk file as exactly eighty bytes.
The Pascal language uses length-prefixed strings. The Macintosh operating system is based on Pascal strings and, as a result, length-prefixed strings are commonly found in files generated from the Macintosh platform. The readLengthPrefixString and writeLengthPrefixString methods are used to read and write length-prefixed strings. The writeLengthPrefixString accepts a string and writes it out to the file. The readLengthPrefixString returns a String object read from the file. Length-prefixed strings occupy their length plus one byte in memory.
The last, and simplest, string type supported by the BinaryFile object is the fixed-width string. A fixed-width string is simply an area of memory reserved for the string. The string occupies the beginning bytes of this buffer and any remaining space is padded with either zeros or spaces. It is not unusual to have to do a trim on a string just read in from this format. The readFixedString and writeFixedString methods are used to read and write fixed-width strings. The readFixedString method accepts a parameter to specify the length of the string and returns a String object read from the file. The writeFixedString method accepts a length parameter and a String object. The String object is then written to the file. If the string is longer than the specified length, then the string is truncated. If the string length is less than the specified length, then the string is padded.












