Why Using FileToStr() to Load a PDF (or Other Binary File) in Visual FoxPro Is a Mistake

1. FoxPro strings are not binary-safe

  • FoxPro strings are designed for text, not arbitrary binary data.
  • When you use FileToStr(file.pdf), FoxPro assumes the bytes in the file represent text, and stores them as a character string.
  • This leads to issues if the binary data includes:
    • Null bytes (0x00) — may terminate the string prematurely
    • Control characters — may be stripped or misinterpreted
    • 8-bit values ≥ 128 — may be re-encoded or altered due to character set conversion

2. Binary data (like PDF files) may not be valid Unicode

  • PDFs often contain arbitrary byte values that do not form valid text encodings (Unicode or otherwise).
  • When FoxPro reads such a file into a string, it may:
    • Corrupt the data by interpreting byte sequences as characters
    • Substitute or drop bytes that don’t map cleanly to Unicode
  • Result: the returned string is not byte-for-byte identical to the original file.

3. Consequences of this mistake

  • When you save that string into a MEMO or CHAR field:
    • The binary data may be irreversibly damaged
    • Exporting it back (e.g. with StrToFile()) may produce a broken PDF
    • Re-encoded characters may not match the original binary at all