Recent

Author Topic: need help with show a file like this  (Read 40822 times)

rvk

  • Hero Member
  • *****
  • Posts: 6658
Re: need help with create a file like this
« Reply #105 on: September 30, 2015, 08:01:51 pm »
AND is an operator its opposite must be an operator as well that given the same input will produce the opposite output the meaning of the input or output has no weight what so ever.
Very true, but if you read back in the topic you see that we were not originally talking about the opposite of bitwise AND but the opposite of the following programming line:
y_offset := (sdate  AND %1111111000000000) shr 9;

You don't even need an AND to "reverse" that line. Just shl and plus. Although... Instead of a plus you could also do it with AND but it is not necessary.

But you're correct in pointing out the fact about opposite of AND.

I'm trying to make it show the Header.Date. Now just crashes. I heard pablo is just lazy checking for errors.
You forgot to read Header.Date. Somehow you removed it.

GMP_47

  • Jr. Member
  • **
  • Posts: 60
Re: need help with create a file like this
« Reply #106 on: September 30, 2015, 08:42:42 pm »
Quote
You forgot to read Header.Date. Somehow you removed it.

like this?
edit: compiles but crashes.
Code: [Select]
program Crear;
uses SysUtils;
type
  THeader = record
    Serial: Word;   //2 bytes
    Filename: String[255];  //1 byte
    Date: Word;  //2 bytes
    Fieldnrs: Word;  //2 bytes
    NrsRecords: Word;  //2 bytes
  end;
  TFields = record
    Fieldnr: Word;   //2 bytes
    Fieldname: ShortString;   //1 byte
  end;
  TRecordField = record
    Fieldnr: Word;  //2 bytes
    FieldText: String[255];  //1 byte
  end;
    TDateTime = double;
var
  BinaryStream: File;
  Header: THeader;                     //Header.Serial Filename Date Fieldnrs NrsRecords
  Fields: array [0..20] of TFields;    //Fields.Fieldnr     Fields.Fieldname
  Rec: array[0..20] of TRecordField;   //Rec.Fieldnr        Rec.FieldText
  Len: Byte;
  nrs: Integer;  //2 bytes
  I,N: Integer;


procedure myRead(var Buffer; Size: Integer);
begin
    BlockRead(BinaryStream, Buffer, Size);
end;

procedure myReadWord(var W:Word);
begin
  myRead(W, 2);
  W := Swap(W);
end;

procedure myReadString(var S:ShortString);
begin
  myRead(Len, 1);
  S[0] := Chr(Len);
  myRead(S[1], Len);
end;

function decode_date ( sdate:word ): TDateTime;
var
  year : integer;
  day, month, y_offset: byte;
begin
     y_offset := (sdate  AND %1111111000000000) shr 9;
     month    := (sdate AND  %0000000111100000) shr 5;
     day      := (sdate  AND %0000000000011111);
 
     if y_offset < 100 then
       year := 2000 + y_offset
     else
       year := 1999 - y_offset + 100;
       result := EnCodeDate(year, month, day);
end;

//function encode_date( stdate :TDateTime ) : word;

begin
  assign(BinaryStream, 'C:\Dev-Pas\EXAMEN2.dat');
  reset(BinaryStream, 1);
  myReadWord(Header.Serial);
  myReadString(Header.Filename);
  myReadWord(Header.Fieldnrs);
  myReadWord(Header.Date);

  for nrs := 0 to Header.Fieldnrs - 1 do
  begin
       myReadWord (Fields[nrs].Fieldnr);
       myReadString (Fields[nrs].Fieldname);
  end;
       myReadWord(Header.NrsRecords);
       Writeln ('Nro. de Serie: ', Header.Serial);
       Writeln ('Full Filename: ', Header.Filename);
       Writeln('Fecha Modificacion: ', datetostr(decode_date(Header.Date)));
       Writeln ('Cantidad de Campos Customizados: ', Header.Fieldnrs);

       for nrs := 0 to Header.Fieldnrs - 1 do
       begin
            Writeln ('Campo [codigo: ',Fields[nrs].Fieldnr,', ','descripcion: ',Fields[nrs].Fieldname ,']' );
       end;
       Writeln ('Cantidad de Registros: ', Header.NrsRecords);
       Writeln ('------------------------');

       for N := 0 to Header.NrsRecords - 1 do
         begin
         myReadWord (Header.Fieldnrs);

                for I := 0 to Header.Fieldnrs - 1 do
                begin
                     myReadWord (Rec[I].Fieldnr);
                     myReadString (Rec[I].FieldText);
                     Writeln (Fields[I].Fieldname,': ',Rec[I].FieldText);
                end;
                Writeln ('------------------------');
       end;
Writeln ('----[FIN CONTENIDO DEL ARCHIVO]-----------------');
Close(BinaryStream);
ReadLn;
end.
« Last Edit: September 30, 2015, 09:19:28 pm by GMP_47 »

rvk

  • Hero Member
  • *****
  • Posts: 6658
Re: need help with create a file like this
« Reply #107 on: September 30, 2015, 11:07:08 pm »
Quote
You forgot to read Header.Date. Somehow you removed it.

like this?
edit: compiles but crashes.
Well. On the question "Like this?" my answer would be... "Does it work?".

So no, it doesn't work.
Look where you put the reading of Header.Date.
It's in the wrong place.

(look again at the order of the fields in your given structure)

shobits1

  • Sr. Member
  • ****
  • Posts: 271
  • .
Re: need help with create a file like this
« Reply #108 on: October 01, 2015, 11:45:21 pm »
Quote
You don't even need an AND to "reverse" that line. Just shl and plus. Although... Instead of a plus you could also do it with AND but it is not necessary.

I agree with about shl (zeroing the left bits), but if you mean you could use plus or AND to merge y_offset, month, day then you are wrong; actually you'll need OR.

BTW, AND and plus don't work the same.

rvk

  • Hero Member
  • *****
  • Posts: 6658
Re: need help with create a file like this
« Reply #109 on: October 01, 2015, 11:53:46 pm »
Quote
You don't even need an AND to "reverse" that line. Just shl and plus. Although... Instead of a plus you could also do it with AND but it is not necessary.

I agree with about shl (zeroing the left bits), but if you mean you could use plus or AND to merge y_offset, month, day then you are wrong; actually you'll need OR.

BTW, AND and plus don't work the same.
That's strange. I have the procedure encode_date written here without OR. I set sdate to 0. I add (y_offset shl 9). I add (month shl 5). And I add day. That's it. Are you saying you can only do it with OR?

Because of the clearing bits at the right you don't need OR at all, I think.


1000 + 0001 = 1001
1100 + 0011 = 1111
If you are sure the bits don't collide in the things you're going to add, adding works just fine. And because we shifted y_offset and month adding should be possible.
(Or I'm I really going crazy here. It's late for me)
« Last Edit: October 02, 2015, 12:00:59 am by rvk »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: need help with create a file like this
« Reply #110 on: October 02, 2015, 12:06:56 am »
Quote
shobits1 wrote
BTW, AND and plus don't work the same.
No, but or'ing and add'ing do _if_ there are no overlapping bits.

Making sure you rule out overlapping bit the and can be used. so afaik rvk is correct in his explanation.

shobits1

  • Sr. Member
  • ****
  • Posts: 271
  • .
Re: need help with create a file like this
« Reply #111 on: October 02, 2015, 03:10:46 am »
rvk, moly; sorry I was wrong, in this particular case OR and plus are practically equal (lack of sleep, can't concentrate  :-[ )
still I don't see how to use AND here, could you explain please.

rvk

  • Hero Member
  • *****
  • Posts: 6658
Re: need help with create a file like this
« Reply #112 on: October 02, 2015, 03:28:55 am »
You don't even need an AND to "reverse" that line. Just shl and plus. Although... Instead of a plus you could also do it with AND but it is not necessary.
still I don't see how to use AND here, could you explain please.
That was only an example if you're really set on creating a "reverse".

So if you have this:
y_offset := (sdate  AND %1111111000000000) shr 9;
you could do this:
sdate := sdate + (y_offset shl 9) AND %1111111000000000;

You could do the AND operation to make absolutely sure you're only dealing with bits 16-10.
But like I said above: The AND operation would not be needed because we already shifted and the bits which appear are always 0.

(If you were not sure about the appearing bits on the right (but we are sure) you would need AND.)
« Last Edit: October 02, 2015, 03:31:07 am by rvk »

derek.john.evans

  • Guest
Re: need help with create a file like this
« Reply #113 on: October 02, 2015, 03:50:22 am »
I dont want to throw a spanner in the works. Here is something which I only just learnt.

http://wiki.freepascal.org/Bit_manipulation

Its been really useful so far. Especially the Bitpacked records

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: need help with create a file like this
« Reply #114 on: October 02, 2015, 04:59:27 am »
@Geepster
For the discussion yes, for OP no, since he uses a very very very very really very old FPC delivered with dev pascal (don't ask).

Be aware with the bitpacked fields though. It can really throw you off when not keeping endianness into mind. It literally becomes bitf*cking then, especially when bits overlap  :D

GMP_47

  • Jr. Member
  • **
  • Posts: 60
Re: need help with create a file like this
« Reply #115 on: October 02, 2015, 11:49:45 pm »
https://youtu.be/EP6G0X5hRkM?t=25s

well... it's done. thanks rvk and everyone.
I wont do the "modify date with last access" thing. I'm going to just do that exclusively for the CREATE.pas.
I'll be studying for an exam on 10/06 and presenting this .pas on 10/07. After that, I'll get started on CREATE.pas. (should I start a new thread?).

b.t.w; just to show you how forgetful pablo is, this was my conversation past wednesday:
me: I was almost done but I couldnt get the date to work.
pablo: np. bring me de .cpp when you get it to work.
me: you mean .pas?
pablo: :hisFaceWasLikeOhGawdNo:
me: I can bring BloodShed Dev-Pas in a drive too.
pablo: yeah. do that.

 

TinyPortal © 2005-2018