• Welcome to Valhalla Legends Archive.
 

dword (UInt32) buffers?

Started by c0ol, September 22, 2004, 03:09 AM

Previous topic - Next topic

c0ol

I am reading file data in using a file stream.  I am reading this data into a Byte array.  I would like to access this data in terms of UInt32s.  In c++ i would simply typecast the buffer, but in c# this does not seem to be the solution.  How can I access this array in terms of UInt32s without copying the data into a UInt32 array with Buffer.BlockCopy(); this method would be far too slow for my needs.

MyndFyre


// compile with /unsafe
private unsafe void doSomethingWithUint32s(byte[] bytes) {
 int nCurPos = 0;
 fixed (uint *pnUint = bytes)
 {
    // use old-fashioned pointer manipulation here.
    while (bytes.Length - nCurPost >= 4)
    {
      // do your thing regarding pnUint.
      pnUint += sizeof(uint); // same as += 4;
      nCurPos += 4;
    }
 }
}
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

c0ol

mm pointers in c#, interesting ;)

K

You could also intialize a System.IO.MemoryStream containing the bytes and read it from there.

BTW, won't that code complain here:

fixed(uint* pnUint = bytes)

that bytes is not a pointer to a uint?

I seem to recall having that problem; the next thing I tried was:

fixed(uint* p = (uint*)bytes)

and it complained that you cannot have a typecast insided a fixed() statement, so I ended up doing:


fixed(byte* bTmp = bytes)
{
   uint* p = (uint*)bTmp;
}


Or maybe I'm crazy  :P

c0ol

well thanks guys, i wasnt even aware you could do pointers in c# lol.  This helps alot!

Skywing

Quote from: c0ol on September 22, 2004, 05:04 PM
well thanks guys, i wasnt even aware you could do pointers in c# lol.  This helps alot!
This isn't always desireable, though.  Your images will be marked "unsafe" / "unverified", so if you are planning to make something that runs in, say, browsers (e.g. like a Java applet) then you would probably not want to use pointers (as "untrusted" code cannot do "unsafe" things, IIRC).

MyndFyre

#6
That's a good point K, heh, sorry I put that up while at work w/o a compiler available.  ;)

Also, you could just use BitConvert.ToUInt32(bytes, i); and increment i by four every iteration.  ;)
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.