Boost logo

Boost :

From: Zara (yozara_at_[hidden])
Date: 2007-10-11 02:56:08

On Thu, 11 Oct 2007 08:40:21 +0200, Roland Schwarz
<roland.schwarz_at_[hidden]> wrote:

>I am searching for a library / component / pattern that will help me
>with the following problem:
>I have a memory structure in raw memory (received from a measurement
>instrument), that is unaligned in its data members.
>Now I want to have an access interface to this data that comes as close
>as possible to access of a struct.
>struct foo {
> char a;
> int b;
>Altough my raw data "char* pdata" has an 8bit followed by a 32bit I want
>to be able to do something like:
>foo data(pdata);
>and then
>char ch = data.a;
>int n = data.b;
>Does anyone know about such a library?
>(This should work compiler/platform independent of course.)

class foo {
    unsigned char internals[5];
    char a() {return internals[0];}
    int b() {
      // select code block depending on your endianness
      unsigned int n=(a[0]<<24)|(a[1]<<16]|(a[2]<<8)|a[3];
      unsigned int n=(a[3]<<24)|(a[2]<<16]|(a[1]<<8)|a[0];
      return static_cast<int>(n);

Boost list run by bdawes at, gregod at, cpdaniel at, john at