#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    1
    Rep Power
    0

    How do you read/write a tbcd?


    Hi,

    I have the following problem :
    I need to convert bcd values to ascii and acsii values to bcd.
    I found the strToBcd and bcdToStr function.
    What I can't figure out is how to read/create a tbcd from a file and write a tbcd to a file.
    I use lazarus at home but rad studio at work.
    The field I have to parse is 6 bytes or char long(on my test file) and it should give me a 11 digit number.

    Does someone have a solution or pointers?

    Regards,
    Seliel
    Last edited by seliel; June 9th, 2013 at 03:51 PM. Reason: bcd is a type so it might be created from read
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    356
    Rep Power
    7
    Don't really know what issues you're having reading/writing a bcd to a file, but possibly if you use a stream to read/write your data, then it would solve the issue.

    First declare a Stream, Lots of stream types to choose from, FileStream, MemoryStream, StringStream, etc...

    I'll use a MemoryStream..

    Code:
    var
       MyBCDValue: TBCD;
       MyStringValue: String;
       LengthOfString: LongInt;
       SaveStream: TMemoryStream;
       ...
    begin
       SaveStream := TMemoryStream.Create;
       try
          //To write to the stream, 
          //you tell it the data you want to 
          //save, and the size of the data
          //you want to save.
          SaveStream.WriteBuffer(MyBCDValue, SizeOf(MyBCDValue));
          //String Types are a bit different.
          //first get the length of the string and 
          //save the length value to the stream
          LengthOfString := Length(MyStringValue);
          SaveStream.WriteBuffer(LengthOfString, SizeOf(LengthOfString));
          //if the string has any length then write it to the stream
          if LengthOfString > 0 then
             SaveStream.WriteBuffer(Pointer(MyStringValue)^, ByteLength(MyStringValue));
          //Then you can save your stream
          SaveStream.SaveToFile('<file location\filename');
       finally
          SaveStream.Free;
       end;
    end;
    To Read from the stream...
    Code:
    Var
       MyBCDValue: TBCD;
       MyStringValue: String;
       LengthOfString: LongInt;
       ReadStream: TMemoryStream;
    begin
       ReadStream := TMemoryStream.Create;
       try
          ReadStream.LoadFromFile('<Data File location\filename>');
          //you need to know the order
          //of your data in your stream
          ReadStream.ReadBuffer(MyBCDValue, SizeOf(MyBCDValue));
          //reading strings is a bit different again
          //read how long your string is.
          ReadStream.ReadBuffer(LengthOfString, SizeOf(LengthOfString));
          SetLength(MyStringValue, LengthOfString);
          if LengthOfString > 0 then
             ReadStream.ReadBuffer(Pointer(MyStringValue)^, ByteLength(MyStringValue));
       finally
          ReadStream.Free;
       end;
    end;

IMN logo majestic logo threadwatch logo seochat tools logo