"Samuel Barber" wrote on 14 Oct 03:
[color=blue]
>
Mark_Kidd@May-Co.com (Mark K) wrote in message[/color]
news:<71c70057.0310130915.701d9408@posting.google. com>...[color=blue][color=green]
> > "Danny Anderson" <bturnip@i.hate.spam> wrote in message[/color][/color]
news:<pan.2003.10.13.06.57.59.594405@i.hate.spam>. ..[color=blue][color=green][color=darkred]
> > > I have an array of shorts that represents a stream of data. This[/color][/color][/color]
data is[color=blue][color=green][color=darkred]
> > > packed in tight, so certain elements may actually contain data[/color][/color][/color]
for more[color=blue][color=green][color=darkred]
> > > than one variable.
> > >
> > > I am to decipher this stream. The first variable packed into[/color][/color][/color]
this array[color=blue][color=green][color=darkred]
> > > happens to be a short, meaning I get away with a simple[/color][/color][/color]
assignment:[color=blue][color=green][color=darkred]
> > > myFirstVariable=myArray[0];
> > >
> > > My problem is that mySecondVariable is a char. What that means[/color][/color][/color]
is that[color=blue][color=green][color=darkred]
> > > the char I need is making up 50% of myArray[1].
> > >
> > > How can I get those 8 bits, and only those 8 bits? This is a[/color][/color][/color]
pretty big[color=blue][color=green][color=darkred]
> > > sticking point, because it follows naturally that[/color][/color][/color]
myThirdVariable starts[color=blue][color=green][color=darkred]
> > > in the second half of myArray[1]. In attempting to come up with[/color][/color][/color]
the right[color=blue][color=green][color=darkred]
> > > answer from this datastream, I have tried all kinds of crazy,
> > > groping-in-the-dark kinds of casts, etc., obviously without any[/color][/color][/color]
luck.[color=blue][color=green]
> >
> >
> > char firstByteData = (myArray[1] & 0xFF00 ) >> 8;
> > char secondByteData = (myArray[1] & 0x00FF) ;[/color]
>
> Congratulations, you hit on the worst possible method.
>
> Solutions are:
>
> (a) If layout is static, use a struct:
> struct mystruct
> {
> short myFirstVariable;
> char mySecondVariable;
> char myThirdVariable;
> };
> (substitute appropriate size-specific typedefs for the C types)
>
> mystruct *myArray = ...
> char mySecondVariable = myArray[i].mySecondVariable;
> char myThirdVariable = myArray[i].myThirdVariable;
>
> (b) Else cast the short* to char*:
> short *myArray = ...
> char mySecondVariable = * (char*) (myArray+i) ;
> char myThirdVariable = * (((char*) (myArray+i))+1) ;
>
> This is the C cast syntax, which also works in C++.
>
> Sam[/color]
Care to _explain_ why bit manipulation is so bad, rather than just
dismissing it?
Would I be right in assuming that your objections revolve around
memory organisation across various architectures? If that's the case,
then it's hardly a reason, the programmer just has to make sure that
data enters the stream in the correct order no matter what machine
it's executed on. I'd much prefer bitwise operations to your second
solution; it's clearer (if properly documented).
Mike
--
Michael Winter
M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)