Page 1 of 1

Posted: Sun Dec 29, 2002 10:33 pm
by BackupUser
Restored from previous forum. Originally posted by ebs.

Can someone please give me an example of how to use a StructureUnion? I use unions in C all the time, but the PB syntax has me baffled. I want to create the PB equivalent to this C union:

typedef union
{
unsigned int CRC;
unsigned int LOW:8;
unsigned int HIGH:8;
} CRC16;

In other words, I want a 16-bit integer and two 8-bit bytes to share the same memory location, so I can manipulate the CRC16 type as one integer or two bytes.

Thank you!

Posted: Sun Dec 29, 2002 10:47 pm
by BackupUser
Restored from previous forum. Originally posted by Pupil.

you could probably do it like this, perhaps there is a better way?

Code: Select all

StructureUnion
  CRC.w
  Byte.b[2]
EndstructureUnion

Posted: Sun Dec 29, 2002 10:56 pm
by BackupUser
Restored from previous forum. Originally posted by ebs.

Pupil,

Yes, I came up with something similar (after I posted the question, of course!):

#LOW = 0
#HIGH = 1

Structure CRC16
StructureUnion
CRC.w
Bytes.b(1)
EndStructureUnion
EndStructure

MyCRC.CRC16

MyCRC\CRC = $1234
Debug MyCRC\CRC
Debug MyCRC\Bytes[#LOW]
Debug MyCRC\Bytes[#HIGH]

What was giving me problems was that I used parentheses "()" for the array in the Debug statements, instead of square brackets "[]". This gave a syntax error. Parentheses seem to work everywhere else I use them for arrays, why not here?

NOW, can someone tell me which is the correct/preferred syntax for arrays:

MyCRC\Bytes[0] or MyCRC\Bytes(0)

Eric


Originally posted by Pupil

you could probably do it like this, perhaps there is a better way?

Code: Select all

StructureUnion
  CRC.w
  Byte.b[2]
EndstructureUnion

Posted: Sun Dec 29, 2002 11:12 pm
by BackupUser
Restored from previous forum. Originally posted by freak.

> NOW, can someone tell me which is the correct/preferred syntax for arrays:

Infos on Arrays:

Arrays declared with the DIM Statement:

Here '()' are used, on declaration and also when accessing the Values.
These Arrays are always Global, no matter if you Dim them in or
outside of a Procedure.
These Arrays can be Resize by another Dim statement. Note: All Data will be lost when calling Dim again.
They can be dimed using Variables.
These Arrays can be MultiArrays, like 'Array(10,20)'
If you write 'Dim Array(10)' you have an Array with 11 Members (0-10)


Arrays inside Structures:

Here '[]' Must be used all the time.
These Arrays can't be resized, they must be defined in the Procedure using a Number, or a Constant, not a Variable!
These Arrays can't be MultiArrays.
IF you write 'Array.l[10]' you get an Array with 10 Members (0-9)
This is a major difference to the other Arrays (caused to be compatible to WinAPI)


That's all i know so far...

Timo


Ha, this was my 250est Post, no i have by 2 Stars back again. :)

Posted: Sun Dec 29, 2002 11:18 pm
by BackupUser
Restored from previous forum. Originally posted by ebs.

Timo,

Thank you! I never understood when "()" should be used vs. "[]" - now I know.

I've filed your explanation away so I can refer to it the next time I get a "mysterious" syntax error!

Regards,
Eric
Originally posted by freak

> NOW, can someone tell me which is the correct/preferred syntax for arrays:

Infos on Arrays:

Arrays declared with the DIM Statement:

Here '()' are used, on declaration and also when accessing the Values.
These Arrays are always Global, no matter if you Dim them in or
outside of a Procedure.
These Arrays can be Resize by another Dim statement. Note: All Data will be lost when calling Dim again.
They can be dimed using Variables.
These Arrays can be MultiArrays, like 'Array(10,20)'
If you write 'Dim Array(10)' you have an Array with 11 Members (0-10)


Arrays inside Structures:

Here '[]' Must be used all the time.
These Arrays can't be resized, they must be defined in the Procedure using a Number, or a Constant, not a Variable!
These Arrays can't be MultiArrays.
IF you write 'Array.l[10]' you get an Array with 10 Members (0-9)
This is a major difference to the other Arrays (caused to be compatible to WinAPI)


That's all i know so far...

Timo

Posted: Mon Dec 30, 2002 3:19 am
by BackupUser
Restored from previous forum. Originally posted by tinman.
Originally posted by Pupil

you could probably do it like this, perhaps there is a better way?
Most Win32 compilers I have used (well, all) have treated an int as 32 bits (therefore as a long). Yes, I see the structure is for 16bits, but it might be worth pointing out :)


PS, freak: that's a beautiful sig for your 250th post :)

--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.40)

Posted: Mon Dec 30, 2002 3:27 am
by BackupUser
Restored from previous forum. Originally posted by ebs.

tinman,

16 bits is correct. The C code I'm converting isn't from Win 32, it's from a compiler for an embedded processor where integers are 16 bits.

Regards,
Eric
Originally posted by tinman
Originally posted by Pupil

you could probably do it like this, perhaps there is a better way?
Most Win32 compilers I have used (well, all) have treated an int as 32 bits (therefore as a long). Yes, I see the structure is for 16bits, but it might be worth pointing out :)


PS, freak: that's a beautiful sig for your 250th post :)

--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.40)

Posted: Mon Dec 30, 2002 4:30 am
by BackupUser
Restored from previous forum. Originally posted by tinman.
Originally posted by ebs
16 bits is correct. The C code I'm converting isn't from Win 32, it's from a compiler for an embedded processor where integers are 16 bits.
In that case, you should also make sure that you get the bitfields in the correct order (which IIRC is compiler dependant).


--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.40)

Posted: Mon Dec 30, 2002 2:57 pm
by BackupUser
Restored from previous forum. Originally posted by ebs.

tinman,

I think this discussion has probably wandered way off-topic, but you sound like an old hand at this! Yes, I made sure to look at the assembly language output to determine what the byte order was. I've got the CRC code working on both the embedded hardware and in PB (there, *now* we're back on topic!). Now all I have to do is coax the PC to send 9-bit (vs. 8-bit) characters, which I'm pretty sure is not possible. Oh, well...

Regards,
Eric
Originally posted by tinman
Originally posted by ebs
16 bits is correct. The C code I'm converting isn't from Win 32, it's from a compiler for an embedded processor where integers are 16 bits.
In that case, you should also make sure that you get the bitfields in the correct order (which IIRC is compiler dependant).


--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.40)