ulong to byte[]

  • Thread starter Thread starter Nadav
  • Start date Start date
Nadav said:
How can a UInt64 be converted to a byte[] ????

See BitConverter.GetBytes(ulong).

And if speed is a factor:
unsafe static byte[] GetBytes(UInt64 value)
{
byte[] bytes = new byte[8];
fixed(byte* b = bytes)
*((UInt64*)b) = value;
return bytes;
}
The BitConverter version has a couple layers of indirection, so its
about 3 times slower (GetBytes(value)>GetUInt64Bytes(value)>internal
runtime call). It's strange, they sometimes do internal calls (for
Integral types) and sometimes they write their own (for floating-point
types).

Austin
 
Back
Top