Why is this program not working? I convert a byte array to long. Then from the long I convert back to a byte array. The resulting byte array is not the same as original.
class Program
{
static void Main(string[] args)
{
byte[] myBytes = { 0, 0, 0, 32, 56, 99, 87, 34, 56, 56, 34, 33, 67
, 56, 66, 72, 1, 0, 0, 56, 0, 22};
long data = BitConverter.ToInt64(myBytes, 0);
byte[] byteData = BitConverter.GetBytes(data);
Console.WriteLine("byte array: " + BitConverter.ToString(myBytes));
Console.WriteLine("byte array: " + BitConverter.ToString(byteData));
}
}
The length of bytes exceed a long
can hold(8 bytes, 64 bits).
For alternative solution, I'd suggest to use BigInteger if your target framework is higher than(including) .Net 4.0.