C# – Append byte array to byte array

During my development a tool to download music file through MMS protocol I had to convert a packet to a byte array and send it through socket to the server. Each packet has its own format and therefore the simple way is to implement each class for each packet and insert a function GetBytes() to each class to convert all fields in packet into byte array. The problem is that the fields have various format and length so it issues a new task how to convert them to byte array and append each other.
To convert the fields to byte array is pretty simple, we just call the static GetBytes() functions of BitConverter class with appropriate arguments as example below

public static byte[] Append(byte rep, byte version, byte versionMinor, byte padding, uint sessionId, uint messageLength, int seal, uint chunkCount, short seq, short MBZ, double timeSent, byte[] MMSmessage)
{
	ByteAppend<byte> fcTemp = new ByteAppend<byte>();
	fcTemp.Add(new byte[] { rep });
	fcTemp.Add(new byte[] { version });
	fcTemp.Add(new byte[] { versionMinor });
	fcTemp.Add(new byte[] { padding });
	fcTemp.Add(BitConverter.GetBytes(sessionId));
	fcTemp.Add(BitConverter.GetBytes(messageLength));
	fcTemp.Add(BitConverter.GetBytes(seal));
	fcTemp.Add(BitConverter.GetBytes(chunkCount));
	fcTemp.Add(BitConverter.GetBytes(seq));
	fcTemp.Add(BitConverter.GetBytes(MBZ));
	fcTemp.Add(BitConverter.GetBytes(timeSent));
	fcTemp.Add(MMSmessage);
	return fcTemp.ToArray();
}

You can see in the code snippet above I convert all native type to byte array easily. However to append them we need to write our own one class so that we can hang each byte array directly behind the previous one.

class ByteAppend<T> : IEnumerable<T>
{
	LinkedList<T[]> m_lstItems = new LinkedList<T[]>();
	int m_nCount;

	public int Count
	{
		get
		{
			return m_nCount;
		}
	}

	public T[] ToArray()
	{
		T[] aTemp = new T[Count];
		for (int nIndex = 0; nIndex < Count; nIndex++)
		{
			aTemp[nIndex] = this[nIndex];
		}
		return aTemp;     
	}

	public void Add(T[] aItems)
	{
		if (aItems == null)
			return;
		if (aItems.Length == 0)
			return;

		m_lstItems.AddLast(aItems);
		m_nCount += aItems.Length;
	}

	private T[] GetItemIndex(int nRealIndex, out int nOffset)
	{
		nOffset = 0; 
		int nCurrentStart = 0; 

		foreach (T[] items in m_lstItems)
		{
			nCurrentStart += items.Length;
			if (nCurrentStart > nRealIndex)
				return items;
			nOffset = nCurrentStart;
		}
		return null;
	}

	public T this[int nIndex]
	{
		get
		{
			int nOffset;
			T[] i = GetItemIndex(nIndex, out nOffset);
			return i[nIndex - nOffset];
		}
		set
		{
			int nOffset;
			T[] i = GetItemIndex(nIndex, out nOffset);
			i[nIndex - nOffset] = value;
		}
	}

	#region IEnumerable<T> Members

	public IEnumerator<T> GetEnumerator()
	{
		foreach (T[] items in m_lstItems)
			foreach (T item in items)
				yield return item;
	}

	#endregion

	#region IEnumerable Members

	System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
	{
		return GetEnumerator();
	}

	#endregion
}

After append them together, we just need to call ToArray() to get the complete byte array back.

3 thoughts on “C# – Append byte array to byte array”

  1. Hi,

    Thanks for providing this C# class to all of us. For some reason though, when I use your ByteAppend class, I get leading zero’s. For example:

    bool myBool = true;
    ushort myInt = 300;

    ByteAppend fcTemp = new ByteAppend();
    fcTemp.Add(BitConverter.GetBytes(myBool));
    fcTemp.Add(BitConverter.GetBytes(myInt));
    fcTemp.ToArray();

    Produces: ’01 2C 01 00 00′

    Do you know what’s wrong? Maybe I implemented it wrong?

  2. @UnK: Please use tag code for adding code. Maybe your initialization is wrong. Use ByteAppend as a type as following

    ByteAppend< byte > fcTemp = new ByteAppend< byte >();

    When I use you code above the result is correct.

    Result

Leave a Reply

Your email address will not be published. Required fields are marked *