Menu

#29 Read binary data from char field

v1.0_(example)
closed
nobody
None
5
2022-02-20
2008-03-16
Wayne
No

I have a datalog file that has an extension .DAT, but is actually a DBF table. One of the fields I want from this table is defined as a C8 field, but is actually a floating point value stored in 64 bits (much like IEE-754). I was previously using the May 2000 release of TDBF which was almost working. I used FieldByName('Value').AsString, however some byte values were being read incorrectly (bytes > 127).

I just downloaded and got the new version working in D4 (thanks to several other forum messages). Now the FieldByName('Value').AsString is returning a null string for all these fields. It appears to stop reading after the first $00 is encountered. I have also tried using the GetFieldData method, but this stops reading after the first $00 as well. I found a similar forum message at http://sourceforge.net/forum/message.php?msg_id=2768447 where mdv suggested:

Instead of: (~line 1528)
StrLCopy(Dst, Src, FieldSize);
try:
Move(Src^, Dst^, FieldSize);
Does that work ? That will copy $0 bytes too.

I have tried this but then I seem to get an error when reading the time field (also a C8 field) that reads extra characters at the end.

Any suggestions on the best way to read these fields?
I have attached the D4 code I currently have, along with some datalog samples (the DAT files are actually CSV files, there is a csv files with the DAT values converted to a C8 field).

Discussion

  • Wayne

    Wayne - 2008-03-16

    Sample D4 code and DAT file

     
  • Wayne

    Wayne - 2008-03-16

    Logged In: YES
    user_id=1556758
    Originator: YES

    Sorry, the DAT files are actually DBF files

     
  • Wayne

    Wayne - 2008-03-16

    Logged In: YES
    user_id=1556758
    Originator: YES

    I have found a fix and this seems to work, however if you have some better ideas or this may cause other problems, please let me know.

    Changed dbf_dbffile line 1757 from:
    ftString:
    StrLCopy(Dst, Src, FieldSize);

    to:
    ftString:
    begin
    Move(Src^, Dst^, FieldSize);
    PChar(Dst)[FieldSize] := #0;
    end;

     
  • Paul Baker

    Paul Baker - 2022-02-20

    You cannot store binary data in a Character (C) field, because the characters are interpreted using the codepage in the header and translated accordingly.

    To store binary data, use TableLevel = 7 and a Binary (B) field (TFieldDef.DataType = ftBinary or TDbfFieldDef.NativeFieldType = 'B').

     
  • Paul Baker

    Paul Baker - 2022-02-20
    • status: open --> closed
    • Group: --> v1.0_(example)
     

Log in to post a comment.