Witam, piszę projekt, gdzie mam mieć 2 funkcje zmieniające znaki ASCII na kod binarny. Pierwsza to wysyłanie z jednego do drugiego komunikatora, a druga to odbiera z jednego do drugiego.
Kod:
// metoda wysylajaca wiadomosc
public void metodaWyslijWiadomosc(JTextPane arg1, JTextPane arg2)
{
char[] tekstDoWyslania = metodaSprawdzSlowa(arg1.getText()).toCharArray();
int START = 1, STOP = 2; // bit startu i bit stopu
char[] tekstDoWyslaniaStartStop = new char[START + STOP + 8 * tekstDoWyslania.length];
for(int i = 0; i < START; i++)
{
tekstDoWyslaniaStartStop[0] = '1';
}
for(int i = 0; i < tekstDoWyslania.length; i++)
{
for(int j = 0; j < 8; j++)
{
tekstDoWyslaniaStartStop[i * 8 + j + START] = (tekstDoWyslania[i] & (1 << (7 - j))) > 0 ? '1' : '0';
}
}
for(int i = 0; i < STOP; i++)
{
tekstDoWyslaniaStartStop[i + START + 8 * tekstDoWyslania.length] = '1';
}
arg2.setText(new String(tekstDoWyslaniaStartStop));
}
// metoda odbierajaca wiadomosc
public void metodaOdbierzWiadomosc(JTextPane arg1, JTextPane arg2, JTextPane arg3)
{
arg2.setText(arg1.getText());
char[] testDoOdebrania = arg2.getText().toCharArray();
int START = 1, STOP = 2;
char[] tekstDoOdebraniaStartStop = new char[(testDoOdebrania.length - START - STOP) / 8];
for(int i = 0; i < tekstDoOdebraniaStartStop.length; i++)
{
for(int j = 0; j < 8; j++)
{
tekstDoOdebraniaStartStop[i] += testDoOdebrania[i * 8 + j + START] == '1' ? 1 << (7 - j) : 0; // przesunięcie
}
}
arg3.setText(new String(tekstDoOdebraniaStartStop));
}
Największy problem mam z tymi linijkami:
tekstDoWyslaniaStartStop[i * 8 + j + START] = (tekstDoWyslania[i] & (1 << (7 - j))) > 0 ? '1' : '0';
oraz
tekstDoOdebraniaStartStop[i] += testDoOdebrania[i * 8 + j + START] == '1' ? 1 << (7 - j) : 0;