Boa tarde,
Fiz uma função que insere bytes de “flag”, gostaria de saber se teria como melhorar esse código. Ele está funcionando perfeitamente, mas haveria uma forma melhor para se fazer?
O byte 0x01 deve virar 0x10,0x21.
O byte 0x04 deve virar 0x10,0x24.
e etc.
Segue código que criei:
[code]public static String byteStuffing(byte[] bytearray){
int count = 0;
//Contando quantos bytes a mais terei no novo array
for (int i = 0; i < bytearray.length; i++) {
if(bytearray[i] == 0x01){
count++;
}else if(bytearray[i] == 0x04){
count++;
}else if(bytearray[i] == 0x10){
count++;
}else if(bytearray[i] == 0x11){
count++;
}else if(bytearray[i] == 0x13){
count++;
}
}
//Criando array secundario que terá os bytes “Codificados” com o tamanho exato
byte[] stuffing = new byte[bytearray.length+count];
count = 0;
//inserindo os bytes “Codificados” no array
for (int i = 0; i < bytearray.length; i++) {
if(bytearray[i] == 0x01){
stuffing[count] = (byte)0x10;
stuffing[count+1]=(byte)0x21;
count++;
}else if(bytearray[i] == 0x04){
stuffing[count] = (byte)0x10;
stuffing[count+1]=(byte)0x24;
count++;
}else if(bytearray[i] == 0x10){
stuffing[count] = (byte)0x10;
stuffing[count+1]=(byte)0x30;
count++;
}else if(bytearray[i] == 0x11){
stuffing[count] = (byte)0x10;
stuffing[count+1]=(byte)0x31;
count++;
}else if(bytearray[i] == 0x13){
stuffing[count] = (byte)0x10;
stuffing[count+1]=(byte)0x33;
count++;
}else{
stuffing[count] = bytearray[i];
}
count++;
}
return toHexString(stuffing);
}[/code]