Lendoria
Lendoria

Reputation: 147

byte[] to string destroying the integers

I created small not finishd Packet Builder class. AddString() working without problems, but if i use AddInt() the console output looks very weird. Any can tell me why the integer not display correctly?

Main

Packet packet = new Packet();
        packet.builder.AddString(Constants.Requests.GET_RESOURCES);
        packet.builder.AddString("Another_String");
        packet.builder.AddInt(500);

        byte[] byteArray = packet.builder.GetByteBuffer();
        Console.WriteLine(ByteArrayToString(byteArray));

        

ByteArray Output: Get_Resources:Another_String:?☺:

47-65-74-5F-52-65-73-6F-75-72-63-65-73-00-3A-41-6E-6F-74-68-65-72-5F-53-74-72-69-6E-67-00-3A-F4-01-00-00-00-3A

As you can see: ?☺ is definitly wrong. The functions are almost the same.

Class

class Packet
    {
        public Builder builder;

        public Packet()
        {
            builder = new Builder();
        }

        private static string ByteArrayToString(byte[] arr)
        {
            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
            return enc.GetString(arr);
        }

        public static string[] Read(byte[] _recievedData)
        {
            string data = ByteArrayToString(_recievedData).Trim();
            string[] result = data.Split(':');

            return result;
        }


        public class Builder
        {

            private byte[] buffer;
            private int offset;

            //Makes very easy on client to filter packets...
            private byte[] seperator;

            public Builder()
            {
                offset = 0;
                buffer = new byte[4096];
                seperator = BitConverter.GetBytes(':');
            }


            public void AddInt(int intValue)
            {
                byte[] byteArray = BitConverter.GetBytes(intValue);
                
                for (int x = 0; x < byteArray.Length; x++)
                {
                    buffer[x + offset] = byteArray[x];
                }

                for (int y = 0; y < seperator.Length; y++)
                {
                    buffer[byteArray.Length + (y + 1) + offset] = seperator[y];
                }

                offset += (byteArray.Length + seperator.Length);
            }

            public void AddString(string str)
            {
                byte[] byteArray = Encoding.ASCII.GetBytes(str);

                for (int x = 0; x < byteArray.Length; x++)
                {
                    buffer[x + offset] = byteArray[x];
                }

                for (int y = 0; y < seperator.Length; y++)
                {
                    buffer[byteArray.Length + (y + 1) + offset] = seperator[y];
                }

                offset += (byteArray.Length + seperator.Length);
            }

            public byte[] GetByteBuffer()
            {
                return buffer;
            }

            public void Reset()
            {
                buffer = null;
                offset = 0;
            }
        }
    
    }

Upvotes: 0

Views: 93

Answers (1)

Ashutosh Raghuwanshi
Ashutosh Raghuwanshi

Reputation: 440

Your code is working perfectly fine. Possibly it is not what you want but following code converts an int in 4 bytes because it is a 32-bit integer.

byte[] byteArray = BitConverter.GetBytes(intValue);

at the end of your output, you see those 4 bytes as expected in little endian format F4-01-00-00 because 500 in hexadecimal is 0x01F4. This explains why you are getting, what you are getting.

Now I am assuming that you are expecting 500 instead of ?☺. Following code should fetch you desired result:

byte[] byteArray = BitConverter.GetBytes(intValue.ToString());

This will add a string representation of the number instead of binary representation. Based on the return type of Read function, the need seems to be a string representation.

Upvotes: 1

Related Questions