Re: syntax for fixed array in C#?

From: Austin Ehlers (liberal*number_eight*_at_ku.edu)
Date: 08/03/04


Date: Mon, 02 Aug 2004 22:02:19 -0500


>
> [StructLayout(LayoutKind.Explicit, Size=120, CharSet=CharSet.Auto)]
> public class Foo
> {
> [FieldOffset(0)] public byte A;
> [FieldOffset(40)] public byte B;
> [FieldOffset(80)] public byte C;
> }
>

<snip>
Assuming the above Foo, this code will work

        Foo foo=new Foo();
        //call Legacy
        string str=null;
        fixed(byte* bP=&foo.A)
        {
                byte[] A=new byte[40];
                for(int x=0;x<A.Length;x++)
                        A[x]=bP[x];
                str=Encoding.ASCII.GetString(A); //if the bytes use
all 8 bits, you'll get bad values. Look at other Encoding.* classes
        }

You can also declare the byte fields as byte* to save a step.

Austin



Relevant Pages

  • Re: Getting class name without instance?
    ... Sibyl wrote: ... > public class Foo { ... > The point is that this same statement needs to be in every class ...
    (comp.lang.java)
  • syntax for fixed array in C#?
    ... It was also a relief to know that I was not overlooking some simple and obvious alternative. ... Austin, thanks for the tip about ASCIIEncoding, too. ...
    (microsoft.public.dotnet.general)