When casting is not enough

Dealing with raw bytes coming across the network has given me plenty of opportunity to figure out how to convert data from one type to another. As previously noted, the good old C method of memcpy'ing bytes into structs takes a bit more work in C#, but is reasonable enough for having the benefit of a GC.

However, when it comes to just taking 4 bytes and turning them into an Int32, i certainly didn't want to jump throuhg those hoops. And you don't have to. Between the static classes of Convert, BitConverter and Encoding.ASCII you can pretty much get any type into any other. This particular case it calls for:

int i = 123456789;
byte[] b = BitConverter.GetBytes(i);
int j = BitConverter.ToInt32(b,0);

I wonder if behind the scenes BitConverter does the whole pinning down of memory game or just does byte arithmetic.

Now, how to define fixed sized strings in structs... I mean i could just create a sub-struct that creates individual slots for each byte it can contain, but that seems as little cumbersome.