About DBase string fields

Today I got a Dbase table that was odd:
When read using tdbf, some string field contained NULL in one record, but an empty string in another record. I was unable to produce such a file with Personal Dbase 8 as well as our internal tool DBaseTableViewer (which despite its name can also change tables, it started out as a simple viewer and uses tdbf to access the files): Deleting the contents of a string field always resulted in NULL. Nonetheless the dbf file looked like this:

null-and-empty-string-fields

As you can see the BEARBEITET field has three different values:
ERROR
[empty string]
[NULL]

Looking at it with a Hex editor I found that a string field is treated as NULL, if it contains all spaces ($20), it is treated as an empty string, if it contains at least one NUL ($00) (the rest can also be spaces), which was the case for the second record. I can recreate the effect by changing the file with said Hex editor.

The source code in tdbf for reading a string field does the following:

  1. Get the field offset and length.
  2. Copy its content to a buffer.
  3. Starting from the end, decrement the length as long as length is > 0 and the lengthth character is a space
  4. If length is now 0, treat the value als NULL, if not, copy it to a string by typecasting the buffer to a PChar

So if the field’s content is:

00 00 00 00 00

it will converted to an empty string.

If the content is:

20 20 20 20 20

it will converted to NULL.

If the content is:

20 00 20 20 20

it will converted to ‘ ‘ (a string containing a single space).

I still have no idea how this file was created though.