Result of Dequeue() byte array is incorrect

longevity uyl 285 Reputation points
2024-01-26T05:41:07.8+00:00

I write the following code to test C# Dequeue(). But result is not correct. Do you have suggestions to fix this issue? byte[] bytes1 = new byte[]{ 0x01, 0x02, 0x03}; byte[] bytes2 = new byte[]{ 0x04, 0x05, 0x06}; CQueue.Enqueue(bytes1); CQueue.Enqueue(bytes2); byte[] bytes12 = CQueue.Dequeue(); Console.WriteLine("{0}", CByteArray.ByteArrayToHexString(bytes1)); Console.WriteLine("{0}", CByteArray.ByteArrayToHexString(bytes2)); Console.WriteLine("{0}", CByteArray.ByteArrayToHexString(bytes12));

namespace Queues
{
    public static class CQueue
    {
        public static Queue<Byte[]> queue = new Queue<Byte[]>();

        public static void Enqueue(byte[] bytes)
        {
            queue.Enqueue(bytes);
        }

        public static byte[] Dequeue()
        {
            if (queue.Count == 0)
            {
                return null;
            }

            return CByteArray.ObjectToByteArray(queue.Dequeue());
        }
    }
}

The following is console snapshot.
010203
040506
0001000000ffffffff01000000000000000f0100000003000000020102030b
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,286 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 119.7K Reputation points
    2024-01-26T06:24:08.62+00:00

    Maybe the ObjectToByteArray function is not needed.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.