Casting question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tom@vandeplas.com

    #1

    Casting question

    Hi All,

    I'm stuck whit an issue I can't seem to resolve in C#:

    I have an arry of bytes which I would like to "recast" to an array of
    structs with an Explicit layout. I tried the Buffer.BlockCop y method, but
    that one complains my struct is not a primitive type.

    Any Suggestions ?

    In detail:
    my struct contains an int and a byte (so nothing special like arrays or so
    ;-), you can maybe best compare it with a point structure)

    The array of bytes is... just an arry of bytes.

    Ideally what I would like to do is assign the address of the last one to the
    address of the first one. But C# doesn't like that anymore (?).

    So I tried Buffer.BlockCop y, but as stated before that doesn't seem to work
    because my struct is not a primitive type.

    So Plan B. Using unsafe code and a fixed statement: I was able to iterate
    through my byte array and cast/copy every struct item one by one. Altough it
    did work, I don't really like this solution as it is consuming quite some
    time (large array) while the memory contents are just identical for both.

    Can somebody help me out on this.


  • Lasse Vågsæther Karlsen

    #2
    Re: Casting question

    > Hi All,[color=blue]
    >
    > I'm stuck whit an issue I can't seem to resolve in C#:
    >
    > I have an arry of bytes which I would like to "recast" to an array of
    > structs with an Explicit layout. I tried the Buffer.BlockCop y method,
    > but that one complains my struct is not a primitive type.[/color]
    <snip>

    Look at the Marshal class.

    --
    Lasse Vågsæther Karlsen

    mailto:lasse@vk arlsen.no
    PGP KeyID: 0x2A42A1C2


    Comment

    • tom@vandeplas.com

      #3
      Re: Casting question

      I did have look at the Marshal.PtrToSt ructure Class, but it only allows me to
      transfor 1 struct a a time, not an array of structs at once.

      Maybe there are some tricks I don't know about Marshal yet ?

      Kindest regrads,

      Tom



      "Lasse Vågsæther Karlsen" wrote:
      [color=blue][color=green]
      > > Hi All,
      > >
      > > I'm stuck whit an issue I can't seem to resolve in C#:
      > >
      > > I have an arry of bytes which I would like to "recast" to an array of
      > > structs with an Explicit layout. I tried the Buffer.BlockCop y method,
      > > but that one complains my struct is not a primitive type.[/color]
      > <snip>
      >
      > Look at the Marshal class.
      >
      > --
      > Lasse Vågsæther Karlsen
      > http://usinglvkblog.blogspot.com/
      > mailto:lasse@vk arlsen.no
      > PGP KeyID: 0x2A42A1C2
      >
      >
      >[/color]

      Comment

      • Lasse Vågsæther Karlsen

        #4
        Re: Casting question

        > I did have look at the Marshal.PtrToSt ructure Class, but it only[color=blue]
        > allows me to transfor 1 struct a a time, not an array of structs at
        > once.
        >
        > Maybe there are some tricks I don't know about Marshal yet ?
        >
        > Kindest regrads,[/color]

        I think you need to marshal them one at a time. Perhaps someone with better
        knowledge of the Marshal class can correct me?

        --
        Lasse Vågsæther Karlsen

        mailto:lasse@vk arlsen.no
        PGP KeyID: 0x2A42A1C2


        Comment

        • Kevin Spencer

          #5
          Re: Casting question

          You may find the following article helpful. While it deals with reading
          structures from binary files (streams), a stream is almost the same as an
          array of bytes:



          --
          HTH,

          Kevin Spencer
          Microsoft MVP
          ..Net Developer
          A brute awe as you,
          a Metallic hag entity, eat us.


          "tom@vandeplas. com" <tomvandeplasco m@discussions.m icrosoft.com> wrote in
          message news:C4FD1170-00BC-4D1F-B8F5-E6BB7F9DA765@mi crosoft.com...[color=blue]
          > Hi All,
          >
          > I'm stuck whit an issue I can't seem to resolve in C#:
          >
          > I have an arry of bytes which I would like to "recast" to an array of
          > structs with an Explicit layout. I tried the Buffer.BlockCop y method, but
          > that one complains my struct is not a primitive type.
          >
          > Any Suggestions ?
          >
          > In detail:
          > my struct contains an int and a byte (so nothing special like arrays or so
          > ;-), you can maybe best compare it with a point structure)
          >
          > The array of bytes is... just an arry of bytes.
          >
          > Ideally what I would like to do is assign the address of the last one to
          > the
          > address of the first one. But C# doesn't like that anymore (?).
          >
          > So I tried Buffer.BlockCop y, but as stated before that doesn't seem to
          > work
          > because my struct is not a primitive type.
          >
          > So Plan B. Using unsafe code and a fixed statement: I was able to iterate
          > through my byte array and cast/copy every struct item one by one. Altough
          > it
          > did work, I don't really like this solution as it is consuming quite some
          > time (large array) while the memory contents are just identical for both.
          >
          > Can somebody help me out on this.
          >
          >[/color]


          Comment

          • Ignacio Machin \( .NET/ C# MVP \)

            #6
            Re: Casting question

            Hi,



            "Kevin Spencer" <kevin@DIESPAMM ERSDIEtakempis. com> wrote in message
            news:e7YqzjfPGH A.1088@tk2msftn gp13.phx.gbl...[color=blue]
            > You may find the following article helpful. While it deals with reading
            > structures from binary files (streams), a stream is almost the same as an
            > array of bytes:[/color]


            As a matter of fact you have MemoryStream which has one constructor that
            receive a byte[]



            --
            Ignacio Machin,
            ignacio.machin AT dot.state.fl.us
            Florida Department Of Transportation


            Comment

            • tom@vandeplas.com

              #7
              Re: Casting question

              Hi,

              I included some code:

              /*
              Sample is a Struct define like this:

              [StructLayout(La youtKind.Explic it,Pack=1,Size= 5)]
              public struct Sample
              {
              [FieldOffset(0)] public UInt32 Tau;
              [FieldOffset(4)] public Byte Value;
              }
              */

              _tempBuffer = new Sample[mSimBuffer[_bufferToUse].Length/5];
              byte[] Bytes = (byte[])mSimBuffer[_bufferToUse];

              //This works, but will convert only 1
              GCHandle _hnd =
              GCHandle.Alloc( mSimBuffer[_bufferToUse],GCHandleType.P inned);
              Object temp =
              Marshal.PtrToSt ructure(_hnd.Ad drOfPinnedObjec t(),typeof(Samp le));
              _hnd.Free();


              //This will throw an error: "The specified structure must be blittable
              or have layout information."
              //Actually I don't really understand this one: didn't I provide enough
              layout info in my struct def?
              GCHandle _hnd2 = GCHandle.Alloc( _tempBuffer,GCH andleType.Pinne d);
              Marshal.Structu reToPtr(Bytes,_ hnd2.AddrOfPinn edObject(),fals e);
              _hnd2.Free();

              "Lasse Vågsæther Karlsen" wrote:
              [color=blue][color=green]
              > > I did have look at the Marshal.PtrToSt ructure Class, but it only
              > > allows me to transfor 1 struct a a time, not an array of structs at
              > > once.
              > >
              > > Maybe there are some tricks I don't know about Marshal yet ?
              > >
              > > Kindest regrads,[/color]
              >
              > I think you need to marshal them one at a time. Perhaps someone with better
              > knowledge of the Marshal class can correct me?
              >
              > --
              > Lasse Vågsæther Karlsen
              > http://usinglvkblog.blogspot.com/
              > mailto:lasse@vk arlsen.no
              > PGP KeyID: 0x2A42A1C2
              >
              >
              >[/color]

              Comment

              • tom@vandeplas.com

                #8
                Re: Casting question

                Hi Kevin,

                Thanks for the reply, using the Marshal.Copy method I was able to get it
                working.

                Do you know if this Copy method will actually copy every byte to another
                memory location, or just set the pointers to the same address ?
                the reason I'm asking this is that I need real good performance...

                Kindest regards,

                Tom



                "Kevin Spencer" wrote:
                [color=blue]
                > You may find the following article helpful. While it deals with reading
                > structures from binary files (streams), a stream is almost the same as an
                > array of bytes:
                >
                > http://csharp.codenewbie.com/article...es-Page_1.html
                >
                > --
                > HTH,
                >
                > Kevin Spencer
                > Microsoft MVP
                > ..Net Developer
                > A brute awe as you,
                > a Metallic hag entity, eat us.
                >
                >
                > "tom@vandeplas. com" <tomvandeplasco m@discussions.m icrosoft.com> wrote in
                > message news:C4FD1170-00BC-4D1F-B8F5-E6BB7F9DA765@mi crosoft.com...[color=green]
                > > Hi All,
                > >
                > > I'm stuck whit an issue I can't seem to resolve in C#:
                > >
                > > I have an arry of bytes which I would like to "recast" to an array of
                > > structs with an Explicit layout. I tried the Buffer.BlockCop y method, but
                > > that one complains my struct is not a primitive type.
                > >
                > > Any Suggestions ?
                > >
                > > In detail:
                > > my struct contains an int and a byte (so nothing special like arrays or so
                > > ;-), you can maybe best compare it with a point structure)
                > >
                > > The array of bytes is... just an arry of bytes.
                > >
                > > Ideally what I would like to do is assign the address of the last one to
                > > the
                > > address of the first one. But C# doesn't like that anymore (?).
                > >
                > > So I tried Buffer.BlockCop y, but as stated before that doesn't seem to
                > > work
                > > because my struct is not a primitive type.
                > >
                > > So Plan B. Using unsafe code and a fixed statement: I was able to iterate
                > > through my byte array and cast/copy every struct item one by one. Altough
                > > it
                > > did work, I don't really like this solution as it is consuming quite some
                > > time (large array) while the memory contents are just identical for both.
                > >
                > > Can somebody help me out on this.
                > >
                > >[/color]
                >
                >
                >[/color]

                Comment

                • rmacias

                  #9
                  Re: Casting question

                  Marshal.Copy copies data from managed memory to unmanaged memory, and vice
                  versa.

                  "tom@vandeplas. com" wrote:
                  [color=blue]
                  > Hi Kevin,
                  >
                  > Thanks for the reply, using the Marshal.Copy method I was able to get it
                  > working.
                  >
                  > Do you know if this Copy method will actually copy every byte to another
                  > memory location, or just set the pointers to the same address ?
                  > the reason I'm asking this is that I need real good performance...
                  >
                  > Kindest regards,
                  >
                  > Tom
                  >
                  >
                  >
                  > "Kevin Spencer" wrote:
                  >[color=green]
                  > > You may find the following article helpful. While it deals with reading
                  > > structures from binary files (streams), a stream is almost the same as an
                  > > array of bytes:
                  > >
                  > > http://csharp.codenewbie.com/article...es-Page_1.html
                  > >
                  > > --
                  > > HTH,
                  > >
                  > > Kevin Spencer
                  > > Microsoft MVP
                  > > ..Net Developer
                  > > A brute awe as you,
                  > > a Metallic hag entity, eat us.
                  > >
                  > >
                  > > "tom@vandeplas. com" <tomvandeplasco m@discussions.m icrosoft.com> wrote in
                  > > message news:C4FD1170-00BC-4D1F-B8F5-E6BB7F9DA765@mi crosoft.com...[color=darkred]
                  > > > Hi All,
                  > > >
                  > > > I'm stuck whit an issue I can't seem to resolve in C#:
                  > > >
                  > > > I have an arry of bytes which I would like to "recast" to an array of
                  > > > structs with an Explicit layout. I tried the Buffer.BlockCop y method, but
                  > > > that one complains my struct is not a primitive type.
                  > > >
                  > > > Any Suggestions ?
                  > > >
                  > > > In detail:
                  > > > my struct contains an int and a byte (so nothing special like arrays or so
                  > > > ;-), you can maybe best compare it with a point structure)
                  > > >
                  > > > The array of bytes is... just an arry of bytes.
                  > > >
                  > > > Ideally what I would like to do is assign the address of the last one to
                  > > > the
                  > > > address of the first one. But C# doesn't like that anymore (?).
                  > > >
                  > > > So I tried Buffer.BlockCop y, but as stated before that doesn't seem to
                  > > > work
                  > > > because my struct is not a primitive type.
                  > > >
                  > > > So Plan B. Using unsafe code and a fixed statement: I was able to iterate
                  > > > through my byte array and cast/copy every struct item one by one. Altough
                  > > > it
                  > > > did work, I don't really like this solution as it is consuming quite some
                  > > > time (large array) while the memory contents are just identical for both.
                  > > >
                  > > > Can somebody help me out on this.
                  > > >
                  > > >[/color]
                  > >
                  > >
                  > >[/color][/color]

                  Comment

                  Working...