Search code examples
c#.netbit-manipulationbitconverter

Converting `UInt16` to byte array


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?!


Solution

  • UInt16 and ushort are the same datatypes so yes

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