Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Moving from byteint array to record

furfur Posts: 9Member
Sorry have not dealt in Pascal in quite some time
so any help appreciated.
[b]
Here is my scenerio:[/b]

bulkrec = record
job : LongInt;
relation : packed array [1..8] of char;
reclen : LongInt;
bulk : array [1.. 1000] of ByteInt;

pbulkrec = ^bulkrec;


[b]and then I have another record like:[/b]

custfile = record
custnum : ShortNat;
group : ShortNat;
name : packed array [1..20] of char;
flag1 : boolean;
flag2 : boolean;
flag3 : boolean;
flag4 : boolean;
end;

pcustfile = ^custfile;


Problem then is that the bulkrec.bulk field actually
holds the custfile data and I want to move it from that
array into custfile record... i.e.

custnum is a 2 byte field so the concatenation of
bulk[1] and bulk[2] holds its value.

group also is a 2 byte field so the concatenation of
bulk[3] and bulk[4] holds its value.

name is 20 byte field and thus
bulk[5] thru bulk[24] hold its value.

flags1-4 hold 1 byte a piece
bulk[25] to bulk[28] respectively.

How do I make this assignment from the bulkrec.bulk
variable to the custfile record???

Any help appreciated.

Comments

  • ActorActor Posts: 438Member
    What is a ByteInt? What is a ShortNat?

    Neither of these terms is part of modern Pascal.

    I'm guessing that
    [code]
    Type
    ByteInt = 0 .. 255 ; { i.e., type Byte }
    ShortNat = -128 .. 127 ;
    [/code]

    By the way the word [b]packed[/b] is obsolete.


  • furfur Posts: 9Member
    Not using modern Pascal.

    ShortNat
    Format of type shortnat
    Total size in bytes: 2
    Type is packed
    range (type:integer) from 0 to 32767

    Format of type byteint
    Total size in bytes: 1
    Type is packed
    range (type:integer) from -128 to 127

  • ActorActor Posts: 438Member
    [code]
    : bulkrec = record
    : job : LongInt;
    : relation : packed array [1..8] of char;
    : reclen : LongInt;
    : bulk : array [1.. 1000] of ByteInt;
    :
    : pbulkrec = ^bulkrec;
    [/code]
    [blue]
    :
    :
    : [b]and then I have another record like:[/b]
    :
    [/blue]
    [code]
    : custfile = record
    : custnum : ShortNat;
    : group : ShortNat;
    : name : packed array [1..20] of char;
    : flag1 : boolean;
    : flag2 : boolean;
    : flag3 : boolean;
    : flag4 : boolean;
    : end;
    :
    : pcustfile = ^custfile;
    [/code]
    [blue]
    :
    :
    : Problem then is that the bulkrec.bulk field actually
    : holds the custfile data and I want to move it from that
    : array into custfile record... i.e.
    :
    : custnum is a 2 byte field so the concatenation of
    : bulk[1] and bulk[2] holds its value.
    :
    : group also is a 2 byte field so the concatenation of
    : bulk[3] and bulk[4] holds its value.
    :
    : name is 20 byte field and thus
    : bulk[5] thru bulk[24] hold its value.
    :
    : flags1-4 hold 1 byte a piece
    : bulk[25] to bulk[28] respectively.
    :
    : How do I make this assignment from the bulkrec.bulk
    : variable to the custfile record???
    :
    : Any help appreciated.
    :
    :
    [/blue]

    The first problem I see is your declarations
    [code]
    pbulkrec : ^bulkrec;
    pcustfile : ^custfile;
    [/code]
    where you declare [b]pbulkrec[/b] and [b]pcustfile[/b] as pointers. The "problem" is that having to reference and de-reference the adds to the complexity and gets in the way of addressing the other issues. I have not idea why you declare these as pointers instead of variables, but I'm going to address your other issues in terms of this declaration:
    [code]
    pbulkrec : bulkrec;
    pcustfile : custfile;
    [/code]
    If you really need these to be pointers I'll leave it to you to make that adjustment.

    [blue]
    : custnum is a 2 byte field so the concatenation of
    : bulk[1] and bulk[2] holds its value.
    [/blue]
    I'm assuming that the definition of [b]BulkRec[/b] is being forced upon you by some existing software or data. If so then the method of "concatenation" is ambiguous and we have to guess how the original programmer did it. The simplest would be
    [code]
    with pbulkrec do
    with pcustfile do
    CustNum := 128*Bulk[1] + Bulk[2] ;
    [/code]
    This assumes that [b]Bulk[1][/b] is the most significant byte, but it could be that [b]Bulk[2][/b] is the MSB.

    However, [b]Bulk[/b] is signed and [b]CustNum[/b] is unsigned. I think this is more likely:
    [code]
    with pbulkrec do
    with pcustfile do
    CustNum := (256*(Bulk[1] + 128) MOD 32768) + (Bulk[2] + 128 ;
    [/code]
    Adding 128 to each [b]Bulk[/b] element shifts it into positive territory, and the MOD operator keeps [b]CustNum[/b] in range. Of course this assumes that the original programmer took this same approach.

    [blue]
    : name is 20 byte field and thus
    : bulk[5] thru bulk[24] hold its value.
    [/blue]
    [code]
    for i := 1 to 20 do
    name[i] := bulk[i + 4] ; [red]{ there may be problems with pack and unpack }[/red]
    [/code]
    [blue]
    : flags1-4 hold 1 byte a piece
    : bulk[25] to bulk[28] respectively.
    [/blue]
    [code]
    flag1 := (bulk[25] = 0) ; [red]{ assume zero means FALSE }[/red]
    flag2 := (bulk[26] = 0) ; [red]{ any else means TRUE }[/red]
    flag3 := (bulk[27] = 0) ;
    flag4 := (bulk[28] = 0) ;
    [/code]

    What do [b]Bulk[29] .. Bulk[1000][/b] hold ?

    What implementation of Pascal are you using? What kind of computer and operating system?

  • furfur Posts: 9Member
    I appreciate your reply...

    However, I am not really looking for help in deciphering how to cast the byte(ints) into the [italic]individual[/italic] fields but was really wondering whether there was some Pascal capability to overlay (or point to) the same data but from the viewpoint of differently defined stuctures and have them correctly "cast" automatically. I may be asking for a ability that does not exist. But simplistically I am looking for:


    custfile:=bulkfile.bulk[1..28];


    As to your final question...in my example what is held in bytes 29-1000 is superfluous to the problem but can be understood if one rationalizes that bulkfile can hold the data in the same way for various other file structures. Thus I could have an address record that is 130 bytes long and when stored in the bulk file it would take up the first 130 bytes of bulkfile.bulk. Note one of the fields of the bulkfile record is "reclen" which would of course hold a 28 in my original example thus allowing the programmer to know how many significant bytes the bulkfile.bulk field held.

    Thanks again...
  • ActorActor Posts: 438Member
    : I
    : may be asking for a ability that does not exist. But simplistically
    : I am looking for:
    :
    :
    : custfile:=bulkfile.bulk[1..28];
    :
    :
    I'm fairly certain that such a thing does not exist in classical Pascals such as described by Jensen and Wirth.

    I think you might be able to do something like this in Turbo Pascal or FreePascal. I'd have to give it some thought as to exactly how.


  • Phat NatPhat Nat Posts: 757Member
    How about Move();

    Just copy all the data from the ByteArray to the start address of the custfile. This should fill in all the data.

    [code]
    Move(bulkfile.bulk, custfile, 28);
    [/code]
Sign In or Register to comment.