Buenas, mi duda es la siguiente estoy tratando de que dada una cadena, es decir, un String subdividirlo en otras subcadenas más pequeñas y que a la última subcadena si no es del tamaño esperado se añada información para que todas las subcadenas tengan el mismo tamaño.
Adjunto código:
import java.io.IOException;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String msg = "En mi casa hay un pája" +
" afsgjsfhghsjkgjsfdhgjkshdgjhsdfjghsgsgsgsfgwhjdsfghjksdfgsdfglksfvdfsjvdsfvunufdig" +
"sfgjsjkfglksjfglkjslkdgjklfgsgdsgdsjglkjsfdgkjslkfjgñlksjflkjgñlsjfgslkm gislkjgjgs" +
"sñlkgjsfijglksjfglkggdgdsgfsjfkjglksjfglksjglkjsfigjskñglmoidsfgslkdmgimdslkgmodsng" +
"lskglksdjgksjgsgsfgflkjdsgkjslkgfjsdgksmgijdflkgmndfjgksgihwlkhigwegt`hewrtgwtnwèip" +
"ljkgdsjgklssgfdkgjwiehg.kergiewñrlkwetñkwjergwer.kjgwerkgwhtñ`whhweñrlkgw0ehgwegp`w" +
"woprgjoiwjroijweroitjowierjtoiwjtjwopmidg09'jhpot'¡0welkgrm8ht'340wjhr80wdfgñlkjdsf" +
"sgffdgsdfgsro que dice hay yaadflñhglfkdngsafgfgjhfglgfhkasjgmv,bserng ahfgajnfsdafa";
Integer longitud = 516; // longitud de las subcadenas en bit
Integer lenmsg = msg.length();
Integer longitudMsgBit =lenmsg.intValue()*32; // multiplico*32 ya que 1 int = 32 bit (java)
//longitud en bit / bit de los submensajes = nº submensajes
Integer numBloquesCodificar = longitudMsgBit / longitud;
System.out.println ("Bloques Bit según tam clave: "+numBloquesCodificar);
String fraccionMsg;
int indexStart = 0;
int indexEnd = 0;
int lenUltimoBloqueBit = 0;
int lenBitAdd= 0;
StringBuffer txt = new StringBuffer();
//se divide el mensaje
for(int i = 0; i <= numBloquesCodificar;i++ ){
//actualizo indices
indexStart = (i*longitud)/(longitud/32);
indexEnd = (longitud + longitud*i)/(longitud/32);
if(i == (numBloquesCodificar - 1)){ //aquí se le añaden los bit de relleno al ultimo bloque
lenUltimoBloqueBit = msg.substring(indexStart).length() * 32;
//calculo los bit del ultimo bloque
lenBitAdd = longitud - lenUltimoBloqueBit;
txt.append(msg.substring(indexStart));
for (int j = 0;j <= lenBitAdd; j++){ //recorremos los bit que falta
txt.append(true); //se lo introducimos mediante boolean
}
fraccionMsg = txt.toString();
}else{
fraccionMsg = msg.substring(indexStart, indexEnd);
}
System.out.println ("fracción "+i+" : "+fraccionMsg);
}
try {
System.in.read();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
¿ Realmente que buscas ?
¿ Dividir el String msg en cadenas mas pequeñas ?
Te voy a dar una pista, un String es una cadena de caracteres, es decir, un conjunto de chars, el String es una clase y tiene metodos, una de ellas es length o largo de la cadena (cantidad de char), tambien hay un metodo llamado toCharArray() que convierte tu guardas los caracteres en un arreglo y de ahi podrias crear otros arreglos acotados.
Podrias leer la documentacion de String, hay metodos bien interesantes.
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html
Saludos