Convert datatype 'long' to byte array
Have you checked BitConverter
long lng =-9999999999L;byte[] mybyt = BitConverter.GetBytes(lng);
hope this is what you are looking
Try to do it in this way:
long l = 4554334;byte[] bA = BitConverter.GetBytes(l);
Be aware that in 2 bytes you can only have 4 full digits + sign, and in 4 bytes you can only have 9 digits + sign, so I had to scale your prereqs accordingly.
public static byte[] SerializeLong2Dec(double value){ value *= 100; value = Math.Round(value, MidpointRounding.AwayFromZero); if (value < -999999999.0 || value > 999999999.0) { throw new ArgumentOutOfRangeException(); } int value2 = (int)value; return BitConverter.GetBytes(value2);}public static double DeserializeLong2Dec(byte[] value){ int value2 = BitConverter.ToInt32(value, 0); return (double)value2 / 100.0;}public static byte[] SerializeLong1Dec(double value) { value *= 10; value = Math.Round(value, MidpointRounding.AwayFromZero); if (value < -999999999.0 || value > 999999999.0) { throw new ArgumentOutOfRangeException(); } int value2 = (int)value; return BitConverter.GetBytes(value2);}public static double DeserializeLong1Dec(byte[] value) { int value2 = BitConverter.ToInt32(value, 0); return (double)value2 / 10.0;}public static byte[] SerializeShort2Dec(double value) { value *= 100; value = Math.Round(value, MidpointRounding.AwayFromZero); if (value < -9999.0 || value > 9999.0) { throw new ArgumentOutOfRangeException(); } short value2 = (short)value; return BitConverter.GetBytes(value2);}public static double DeserializeShort2Dec(byte[] value) { short value2 = BitConverter.ToInt16(value, 0); return (double)value2 / 100.0;}public static byte[] SerializeShort1Dec(double value) { value *= 10; value = Math.Round(value, MidpointRounding.AwayFromZero); if (value < -9999.0 || value > 9999.0) { throw new ArgumentOutOfRangeException(); } short value2 = (short)value; return BitConverter.GetBytes(value2);}public static double DeserializeShort1Dec(byte[] value) { short value2 = BitConverter.ToInt16(value, 0); return (double)value2 / 10.0;}
So that it's clear, the range of a (signed) short (16 bits) is -32,768 to 32,767 so it's quite clear that you only have 4 full digits plus a little piece (the 0-3), the range of a (signed) int (32 bits) is −2,147,483,648 to 2,147,483,647 so it's quite clear that you only have 9 full digits plus a little piece (the 0-2). Going to a (signed) long (64 bits) you have -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 so 18 digits plus a (big) piece. Using floating points you lose in accuracy. A float (32 bits) has an accuracy of around 7 digits, while a double (64 bits) has an accuracy of around 15-16 digits.