Csharp program which i am trying to make which fits these instructions: Work continues on the remote control car project. Bandwidth in the telemetry system is at a premium and you have been asked to implement a message protocol for communicating telemetry data.
Data is transmitted in a buffer (byte array). When integers are sent, the size of the buffer is reduced by employing the protocol described below. My code returns every byte correct in the list except the first item.
i tried seperating the conversions inot the smallest type and then into a BitConverter.GetBytes() function.
using System;
public static class TelemetryBuffer
{
public static byte[] ToBuffer(long a)
{
if (9223372036854775807 >= a && a > 4294967296)
{
long num = (long)a;
byte[] bytes = new byte[8];
bytes = BitConverter.GetBytes(num);
return bytes;
}
else if (4294967296 >= a && a > 2147483648)
{
uint num = (uint)a;
byte[] bytes = new byte[8];
bytes = BitConverter.GetBytes(num);
return bytes;
}
else if (2147483647 >= a && a > 65536)
{
int num = (int)a;
byte[] bytes = new byte[8];
bytes = BitConverter.GetBytes(num);
return bytes;
}
else if (65535 >= a && a > 0)
{
ushort num = (ushort)a;
byte[] bytes = new byte[8];
bytes = BitConverter.GetBytes(num);
return bytes;
}
else if (-1 >= a && a > -32768)
{
short num = (short)a;
byte[] bytes = new byte[8];
bytes = BitConverter.GetBytes(num);
return bytes;
}
else if (-2147483648 < a && a <= -32769)
{
int num = (int)a;
byte[] bytes = new byte[8];
bytes = BitConverter.GetBytes(num);
return bytes;
}
else if (-9_223_372_036_854_775_808 < a && a <= -2_147_483_649)
{
long num = (long)a;
byte[] bytes = new byte[8];
bytes = BitConverter.GetBytes(num);
return bytes;
}
else
{
int num = (int)a;
byte[] bytes = new byte[8];
bytes = BitConverter.GetBytes(num);
return bytes;
}
//throw new NotImplementedException("Please implement the static TelemetryBuffer.ToBuffer() method");
}
public static long FromBuffer(byte[] buffer)
{
throw new NotImplementedException("Please implement the static TelemetryBuffer.FromBuffer() method");
}
}
Assert.Equal() Failure
↓ (pos 0)
Expected: [248, 255, 255, 255, 255, ...]
Actual: [255, 255, 255, 255, 255, ...]
↑ (pos 0)
Assert.Equal() Failure
↓ (pos 0)
Expected: [248, 0, 0, 0, 0, ...]
Actual: [0, 0, 0, 0]
↑ (pos 0)
Assert.Equal() Failure
↓ (pos 0)
Expected: [4, 0, 0, 0, 128, ...]
Actual: [0, 0, 0, 128]
↑ (pos 0)