Recent

Author Topic: ftBytes in TBufDataset  (Read 761 times)

ojz0r

  • Jr. Member
  • **
  • Posts: 63
ftBytes in TBufDataset
« on: June 26, 2024, 02:59:40 pm »
I'm trying to use ftBytes as fieldtype in a BufDataset but it doesn't behave the way i expect it to do.
When i set the Size attribute of the field to 1 i expect it to allocate 1 byte, so max value 255 (or $FF). However it seems that "Size" corresponds to number of digits, if i try to enter 123 it will only keep 1, if i set size to 2 it will keep 12, and if i set size to 3 it will keep 123. However that means that it will accept values up to 999.

Is there something im doing wrong or is this a bug?
I've tried to search for "ftBytes" and "ftByte" but i can't find anything relevant.
I like big endians and i can not lie.

Zvoni

  • Hero Member
  • *****
  • Posts: 2625
Re: ftBytes in TBufDataset
« Reply #1 on: June 26, 2024, 03:53:19 pm »
https://www.freepascal.org/docs-html/fcl/db/tfieldtype.html

The only thing coming close would be ftSmallInt, but that's signed
ftBytes is an Array of Bytes
Quote
*snip*
ftTime,  Time value
ftDateTime, Date/Time (timestamp) value
ftBytes,  Array of bytes value, fixed size (unytped)
ftVarBytes, Array of bytes value, variable size (untyped)
ftAutoInc, Auto-increment integer value (4 bytes)
*snip*

That said: Set the "size" to 0
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

ojz0r

  • Jr. Member
  • **
  • Posts: 63
Re: ftBytes in TBufDataset
« Reply #2 on: June 26, 2024, 08:22:29 pm »
Okay so no real byte type then.
Smallint seems variable depending on CPU architecture. For 64-bit system a Smallint is 16-bit signed integer, so +/-32764.
I like big endians and i can not lie.

Richard Marriott

  • Newbie
  • Posts: 4
Re: ftBytes in TBufDataset
« Reply #3 on: September 03, 2024, 04:19:07 am »
As already mentioned ftBytes represents an array of byte ( defined as TBytes), I manged to get it to work with this code:

      FieldDefs.Add('XBYTES',ftBytes,1,false);  //1I byte in length

Then to add/retreive data after CreateTable or CreateDataset do this

     Setlength(tb,1);                                              // tb is type TBytes 
     tb[0] := b;                                                       // where b is a number of type byte (0..255)
     FieldsByName('XBYTES').AsBytes := tb;     // Save the value to the buffer
   
     tb2 := FieldsByName('XBYTES').AsBytes;  // Retrieve the value into tb2 which is type TBytes     
     b2 := tb2[0];                                                 // Same as original input
   
If you do this (as TDBGrid would do)
    s := FieldsByName('XBYTES'].AsString;     // s is chr(b2)  ie the character equivalent

Hope that helps. Else as already suggested use ftSmallInt (2 bytes) with external code to do range checking. I think that would be simpler. ftBytes is really best for binary arrays where you need to know exactly what is contained in each byte and process each byte separately.
 

Richard Marriott

  • Newbie
  • Posts: 4
Re: ftBytes in TBufDataset
« Reply #4 on: September 03, 2024, 04:53:08 am »
Just had a better idea that works and is simpler. Try this:

      FieldDefs.Add('XBYTES',ftBytes,1,false);  //1 byte in length

Then to add/retrieve data after CreateTable or CreateDataset do this

     b1 := 123;                                                        // b1 is type byte. assign any valid number 
     FieldsByName('XBYTES').SetData(@b1);     // Save the value to the buffer
   
    FieldsByName('XBYTES').GetData(@b2);  // Retrieve the value into b2 of type byte  result is 123 
 

 

TinyPortal © 2005-2018