Scusate mi sono accorto che era solo un piccolo errore concettuale..
Ho risolto, ma se volete scrivo qui il codice completo che magari potrebbe essere utile a qualcun altro
codice:
byte[] buffer = UTF8Encoding.UTF8.GetBytes(sToSend);
//ora suddividiamola in vari blocchi da 4 byte
//bisogna rendere il numero d bytes un multiplo di 4
//trovo il resto della divisione
int res = buffer.Length % 4;
//ora a seconda dei casi aggiungo i bytes necessari
//buffer finale
byte[] buffer2;
switch (res)
{
case 0:
//va bene cosi
buffer2 = buffer;
break;
case 1:
//si aggiungono 3 bytes vuoti
//nuovo array della giusta lunghezza
buffer2 = new byte[buffer.Length +3];
//copia
Buffer.BlockCopy(buffer,0,buffer2 ,0,buffer.Length);
//aggiunta byte vuoti
buffer2[buffer2.Length - 3] = 0;
buffer2[buffer2.Length - 2] = 0;
buffer2[buffer2.Length - 1] = 0;
break;
case 2:
//si aggiungono 2 bytes
buffer2 = new byte[buffer.Length +2];
//copia
Buffer.BlockCopy(buffer,0,buffer2 ,0,buffer.Length);
//aggiunta byte vuoti
buffer2[buffer2.Length - 2] = 0;
buffer2[buffer2.Length - 1] = 0;
break;
case 3:
//si aggiungono 2 bytes
buffer2 = new byte[buffer.Length + 1];
//copia
Buffer.BlockCopy(buffer, 0, buffer2, 0, buffer.Length);
//aggiunta byte vuoti
buffer2[buffer2.Length - 1] = 0;
break;
default:
buffer2 = buffer;
break;
}
//arrayList di oggetti tdp
ArrayList TDPtoSend = new ArrayList();
//ora con un ciclo si dividono in pacchetti TDP
for (int i = 0; i <= buffer2.Length - 5; i = i + 4)
{
//array di 4 byte
byte[] buff = new byte[4];
buff[0] = buffer2[i];
//si controlla di non uscire da buffer
buff[1] = buffer2[i + 1];
buff[2] = buffer2[i + 2];
buff[3] = buffer2[i + 3];
//si crea un nuovo oggetto TDP e lo si aggiunge
TDP New = new TDP(buff);
TDPtoSend.Add(New);
}
grazie comunque per l'attenzione!