1

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)
Flydog57
  • 6,851
  • 2
  • 17
  • 18
DMMAHMOOD
  • 11
  • 1
  • Two points to make your code more readable: a) consider using an underscore (`_`) as a numeric separator in more places, an b) use hex or binary numbers instead of decimal - they'd make more sense in this context – Flydog57 Aug 30 '23 at 04:30
  • Even better, use the built-in constants `int.MaxValue`, `byte.MaxValue`, etc – Andrew Williamson Aug 30 '23 at 04:41
  • Invert your logic, check for the smallest sizes first and work your way up. That way you don't need a lower bound check in each 'if' statement – Andrew Williamson Aug 30 '23 at 04:43
  • Please be aware that `BitConverter` can output the bytes in a different order depending on the [endianness of the CPU](https://en.wikipedia.org/wiki/Endianness). This may work on your computer, but fail when you submit it. For a host-agnostic implementation, you need to explicitly specify which endianness you want to use. Have a look at the [BinaryPrimitives class](https://learn.microsoft.com/en-us/dotnet/api/system.buffers.binary.binaryprimitives?view=net-7.0) – Andrew Williamson Aug 30 '23 at 04:50
  • I also don't recommend both variable-width packing _and_ signed + unsigned types, without adding a type identifier somewhere in the message header/metadata. If you receive a 4-byte message, how do you know if it is a signed or an unsigned number? – Andrew Williamson Aug 30 '23 at 04:52
  • 1
    It looks to me like there's a more complete specification of this communication protocol and that *includes prepending a byte to tell you what the next part of the buffer will look like*. You've forgotten to implement that bit and you've forgotten to include it in the question. (Note, in your third example, how `128` is expected at position 4, not 3, which indicates that all of your output is meant to be shifted). Also, no need to do `byte[] bytes = new byte[8]` when `GetBytes` is always going to give you a *new* array in return. – Damien_The_Unbeliever Aug 30 '23 at 05:47

0 Answers0