Rabbit
Rabbit

Reputation: 1821

Scanning byte array as uint array

I have a large byte array with mostly 0's but some values that I need to process. If this was C++ or unsafe C# I would use a 32bit pointer and only if the current 32bit were not 0, I would look at the individual bytes. This enables much faster scanning through the all 0 blocks. Unfortunately this must be safe C# :-)

I could use an uint array instead of a byte array and then manipulate the individual bytes but it makes what I'm doing much more messy than I like. I'm looking for something simpler, like the pointer example (I miss pointers sigh)

Thanks!

Upvotes: 2

Views: 1054

Answers (4)

Jim Mischel
Jim Mischel

Reputation: 133975

You can use the BitConverter class:

byte[] byteArray = GetByteArray(); // or whatever
for (int i = 0; i < b.Length; I += 2)
{
    uint x = BitConverter.ToUInt32(byteArray, i);
    // do what you want with x
}

Another option is to create a MemoryStream from the byte array, and then use a BinaryReader to read 32-bit values from it.

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062550

If the code must be safe, and you don't want to use a larger type and "shift", them you'll have to iterate each byte.

(edit) If the data is sufficiently sparse, you could use a dictionary to store the non-zero values; then finding the non-zeros is trivial (and enormous by sparse arrays become cheap).

Upvotes: 2

John Zwinck
John Zwinck

Reputation: 249113

I'd follow what this guy said: Using SSE in c# is it possible?

Basically, write a little bit of C/C++, possibly using SSE, to implement the scanning part efficiently, and call it from C#.

Upvotes: 2

sehe
sehe

Reputation: 392853

You can access the characters

string.ToCharArray()

Or you can access the raw byte[]

Text.Encoding.UTF8Encoding.GetBytes(stringvalue)

Ultimately, what I think you'd need here is

MemoryStream stream;
stream.Write(...)

then you will be able to directly hadnle the memory's buffer

There is also UnmanagedMemoryStream but I'm not sure whether it'd use unsafe calls inside

Upvotes: 0

Related Questions