Convert from byte[] to structure value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • moni

    Convert from byte[] to structure value

    Hey,

    My buffer contains a short int, some char[], and a structure in form of
    a byte array.


    Read the string as:
    TextBox4.Text = System.Text.Enc oding.ASCII.Get String(buffer1, 0, 31);

    Read the int as:
    TextBox2.Text = BitConverter.To Int16(buffer,0) .ToString();

    How do I get the structure value to display.

    Thanks alot..

  • Michael Bray

    #2
    Re: Convert from byte[] to structure value

    "moni" <mons.2110@gmai l.comwrote in news:1155145116 .321730.52460
    @b28g2000cwb.go oglegroups.com:
    How do I get the structure value to display.
    >
    >
    You have to read each value of the structure in order it is stored in the
    array. If you are the one writing the byte array, then you could also look
    into serializing / deserializing the structure.

    -mdb

    Comment

    • moni

      #3
      Re: Convert from byte[] to structure value




      Marshal.Copy( new IntPtr( pBuffer.ToInt32 ()+ 71), buffer1, 0, 4 );

      I am copying 4 bytes which are part of my structure into buffer1 which
      is a byte array.
      Here pBuffer is a ptr to the Memory Mapped File I have.

      In order to read from this array I am doing this:

      TextBox6.Text = BitConverter.To UInt32(buffer1, 0).ToString();

      But it is giving me garbage values.

      Any clue?

      Thanks


      Michael Bray wrote:
      "moni" <mons.2110@gmai l.comwrote in news:1155145116 .321730.52460
      @b28g2000cwb.go oglegroups.com:
      >
      How do I get the structure value to display.
      >
      You have to read each value of the structure in order it is stored in the
      array. If you are the one writing the byte array, then you could also look
      into serializing / deserializing the structure.
      >
      -mdb

      Comment

      • Jim H

        #4
        Re: Convert from byte[] to structure value

        Could there be an endian problem? Is the data coming from another machine
        or was the data put in network byte order before being sent?

        jim

        "moni" <mons.2110@gmai l.comwrote in message
        news:1155149536 .523423.157210@ b28g2000cwb.goo glegroups.com.. .
        >
        >
        >
        Marshal.Copy( new IntPtr( pBuffer.ToInt32 ()+ 71), buffer1, 0, 4 );
        >
        I am copying 4 bytes which are part of my structure into buffer1 which
        is a byte array.
        Here pBuffer is a ptr to the Memory Mapped File I have.
        >
        In order to read from this array I am doing this:
        >
        TextBox6.Text = BitConverter.To UInt32(buffer1, 0).ToString();
        >
        But it is giving me garbage values.
        >
        Any clue?
        >
        Thanks
        >
        >
        Michael Bray wrote:
        >"moni" <mons.2110@gmai l.comwrote in news:1155145116 .321730.52460
        >@b28g2000cwb.g ooglegroups.com :
        >>
        How do I get the structure value to display.
        >
        >
        >>
        >You have to read each value of the structure in order it is stored in the
        >array. If you are the one writing the byte array, then you could also
        >look
        >into serializing / deserializing the structure.
        >>
        >-mdb
        >

        Comment

        • moni

          #5
          Re: Convert from byte[] to structure value

          The data here is being written into a memory mapped file by a service
          that is running on the machine. My GUI program is reading the data from
          the Memory mapped file.

          I dont think it can be an endian problem because I have managed to read
          all other string values, short int values correctly .

          Am I reading the structure value correctly when I do,

          TextBox6.Text = BitConverter.To UInt32(buffer1, 0).ToString();

          where,

          Marshal.Copy( new IntPtr( pBuffer.ToInt32 ()+ 71), buffer1, 0, 4 );

          is copying 4 btes of data onto buffer1 from the ptr pointed to the MMF.

          Thanks.



          Jim H wrote:
          Could there be an endian problem? Is the data coming from another machine
          or was the data put in network byte order before being sent?
          >
          jim
          >
          "moni" <mons.2110@gmai l.comwrote in message
          news:1155149536 .523423.157210@ b28g2000cwb.goo glegroups.com.. .



          Marshal.Copy( new IntPtr( pBuffer.ToInt32 ()+ 71), buffer1, 0, 4 );

          I am copying 4 bytes which are part of my structure into buffer1 which
          is a byte array.
          Here pBuffer is a ptr to the Memory Mapped File I have.

          In order to read from this array I am doing this:

          TextBox6.Text = BitConverter.To UInt32(buffer1, 0).ToString();

          But it is giving me garbage values.

          Any clue?

          Thanks


          Michael Bray wrote:
          "moni" <mons.2110@gmai l.comwrote in news:1155145116 .321730.52460
          @b28g2000cwb.go oglegroups.com:
          >
          How do I get the structure value to display.


          >
          You have to read each value of the structure in order it is stored in the
          array. If you are the one writing the byte array, then you could also
          look
          into serializing / deserializing the structure.
          >
          -mdb

          Comment

          • Larry Smith

            #6
            Re: Convert from byte[] to structure value

            In C/C++, you're guaranteed that fields are stored in memory in the
            order they're declared. For example, (assuming x86) in

            class foo {
            int x;
            double y;
            int z;
            };

            the memory layout will be x, then y, then z. But as you may be aware,
            there may be alignment issues. Ignoring vtable issues and the like, x is
            at offset 0, but y probably isn't at offset 4, but at offset 8 (and z at
            offset 16). Some CPU architectures perform better if data is aligned on
            "natural boundaries" (and some will actually abort if the data isn't
            aligned). So doubles are preferentially stored at addresses that are a
            multiple of 8, and there would be 4 bytes of padding (uninitialized,
            ignored data) between x and y. You must (again, talking just C/C++) use
            some extra-language feature (usually #pragma) to adjust (e.g. eliminate)
            these pad bytes).

            C# addresses this issue by ***not*** guaranteeing that data is stored in
            the order you declare. In the above case, it might store data in memory
            in the order y, x, z. Or even y, z, x. Or x, z, y (or z, x, y). All of
            which satisfy alignment rules, and save 4 bytes of padding per instance.

            To get around this, prefix your class with
            [StructLayout(La youtKind.Sequen tial)]
            class foo ...

            You'll also need a
            using System.Runtime. InteropServices ;
            for the StructLayoutAtt ribute et al.

            HTH

            P.S. Side note: Without StructLayout, the alignments are *not*
            determined at compile time, since the compiler doesn't know if this will
            be run on a 32-bit architecture, 64-bit, different CPU families (x86,
            PowerPC, etc), all of which may have different alignment rules.

            moni wrote:
            The data here is being written into a memory mapped file by a service
            that is running on the machine. My GUI program is reading the data from
            the Memory mapped file.
            >
            I dont think it can be an endian problem because I have managed to read
            all other string values, short int values correctly .
            >
            Am I reading the structure value correctly when I do,
            >
            TextBox6.Text = BitConverter.To UInt32(buffer1, 0).ToString();
            >
            where,
            >
            Marshal.Copy( new IntPtr( pBuffer.ToInt32 ()+ 71), buffer1, 0, 4 );
            >
            is copying 4 btes of data onto buffer1 from the ptr pointed to the MMF.
            >
            Thanks.
            >
            >
            >
            Jim H wrote:
            >Could there be an endian problem? Is the data coming from another machine
            >or was the data put in network byte order before being sent?
            >>
            >jim
            >>
            > "moni" <mons.2110@gmai l.comwrote in message
            >news:115514953 6.523423.157210 @b28g2000cwb.go oglegroups.com. ..
            >>>
            >>>
            >>Marshal.Cop y( new IntPtr( pBuffer.ToInt32 ()+ 71), buffer1, 0, 4 );
            >>>
            >>I am copying 4 bytes which are part of my structure into buffer1 which
            >>is a byte array.
            >>Here pBuffer is a ptr to the Memory Mapped File I have.
            >>>
            >>In order to read from this array I am doing this:
            >>>
            >>TextBox6.Te xt = BitConverter.To UInt32(buffer1, 0).ToString();
            >>>
            >>But it is giving me garbage values.
            >>>
            >>Any clue?
            >>>
            >>Thanks
            >>>
            >>>
            >>Michael Bray wrote:
            >>>"moni" <mons.2110@gmai l.comwrote in news:1155145116 .321730.52460
            >>>@b28g2000cwb .googlegroups.c om:
            >>>>
            >>>>How do I get the structure value to display.
            >>>>>
            >>>>>
            >>>You have to read each value of the structure in order it is stored in the
            >>>array. If you are the one writing the byte array, then you could also
            >>>look
            >>>into serializing / deserializing the structure.
            >>>>
            >>>-mdb
            >

            Comment

            Working...