Scott Hanselman

And the answer shall comethis is itnbsp This is why I love the hell out of NETnbsp I tell this to my students when

October 08, 2002 Comment on this post [0] Posted in Web Services
Sponsored By

And the answer shall come...this is it.  This is why I love the hell out of .NET.  I tell this to my students when I teach .NET, but each day I use the Framework I start to live it even more.  Sure, there are things you fight with, there are things you hate, but really when it comes down to it: A LOT of good thought was put into the Framework.  There are Utility Classes galore.  (Of course, there's no HashMap, but that's another day)

What I did in a cheesy moment (a 3am moment) of frustration:

public unsafe static byte[] UIntToBytes(uint UIntIn)
{
    //turn a uint32 into 4 bytes
    byte[] fourBytes = new byte[4];
    uint* pt = &UIntIn;
    byte* bt = (byte*)&pt[0];
    fourBytes[0] = *bt++;
    fourBytes[1] = *bt++;
    fourBytes[2] = *bt++;
    fourBytes[3] = *bt++;
    return fourBytes;
}

Here's what it looks like now (in VB.NET):

Public Shared Function IntToBytes(ByVal IntIn As Integer) As Byte()
   
Return BitConverter.GetBytes(IntIn)
End Function

I can't believe I stooped to writing unsafe :) code to do something as simple as getting the Bytes out of an Integer.  Fool me once, shame on you.  Fool me twice, shame on me.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.