Converting `UInt16` to byte array

Saeid Yazdani picture Saeid Yazdani · Oct 12, 2012 · Viewed 8.2k times · Source

How to convert an UInt16 valu to a byte array of size 2

I found some code in MSDN but I am not sure if it is ok to use in my case:

// Convert a ushort argument to a byte array and display it. 
public static void GetBytesUInt16( ushort argument )
{
    byte[ ] byteArray = BitConverter.GetBytes( argument );
    Console.WriteLine( formatter, argument, 
        BitConverter.ToString( byteArray ) );
}

What I have is UInt16 ... is it ok to just cast it to ushort and use the code above?!

Answer

Dave Lawrence picture Dave Lawrence · Oct 12, 2012

UInt16 and ushort are the same datatypes so yes

http://msdn.microsoft.com/en-us/library/cbf1574z(v=vs.80).aspx