• Sábado 21 de Septiembre de 2024, 14:23

Autor Tema:  Convertir String A Bytes  (Leído 903 veces)

JohanLohan

  • Nuevo Miembro
  • *
  • Mensajes: 7
    • Ver Perfil
Convertir String A Bytes
« en: Viernes 7 de Julio de 2006, 18:20 »
0
Tengo un problema, y este es que tengo un archivo del cual leo y guardo en un string.
Este archivo se compone de 1's y 0's, y lo que quiero hacer es coger esos unos y ceros y convertirlos a bytes, pero no los caracteres, lo que quiero es coger los 8 primeros 1' y 0's y ese sera mi byte, y despues los siguientes mi otro byte, y asi. Para ello utilizo la siguiente clase:

import java.util.*;
import java.io.*;

class StringToArrayList{
   
    String rawData;
    ArrayList <Byte>binaryEncodedData2 = new ArrayList<Byte>();
    Hashtable <String,Byte>encodingBitMap = new Hashtable<String,Byte>();
   
  //-----------------------------------------------------//     
    //Method to convert the file OutBinary.txt in an ArrayList<Byte>
    //to after the conversion call from the main the HuffmanDecoder class.

    ArrayList<Byte> encode2(String rawData){
    //Save the incoming parameters.
    this.rawData = rawData;
   
    int a = binaryEncodedData2.size();
   System.out.println("Elementos en la cadena: "+a);
   
   buildEncodingBitMap();
   encodeStringToBits();  
    //Return the encoded message.
    return binaryEncodedData2;
 }//End of encode2
 
   //-----------------------------------------------------//

  void buildEncodingBitMap(){

    for(int cnt = 0; cnt <= 255;cnt++){
      StringBuffer workingBuf = new StringBuffer();
      if((cnt & 128) > 0){workingBuf.append("1");
        }else{workingBuf.append("0");};
      if((cnt & 64) > 0){workingBuf.append("1");
        }else {workingBuf.append("0");};
      if((cnt & 32) > 0){workingBuf.append("1");
        }else {workingBuf.append("0");};
      if((cnt & 16) > 0){workingBuf.append("1");
        }else {workingBuf.append("0");};
      if((cnt & 8) > 0){workingBuf.append("1");
        }else {workingBuf.append("0");};
      if((cnt & 4) > 0){workingBuf.append("1");
        }else {workingBuf.append("0");};
      if((cnt & 2) > 0){workingBuf.append("1");
        }else {workingBuf.append("0");};
      if((cnt & 1) > 0){workingBuf.append("1");
        }else {workingBuf.append("0");};
      encodingBitMap.put(workingBuf.toString(),(byte)(cnt));
    }//end for loop
  }//end buildEncodingBitMap
  //-----------------------------------------------------//
   
  void encodeStringToBits(){
   
    //Extract the String representations of the required
    // eight bits.  Generate eight actual matching bits by
    // looking the bit combination up in a table.
    for(int cnt = 0;cnt < rawData.length();cnt += 8){
       
      String strBits  = rawData.substring(cnt,cnt+8);
      System.out.println("Elementos en strBits: "+strBits);
      byte realBits = encodingBitMap.get(strBits);
      binaryEncodedData2.add(realBits);
      int b = binaryEncodedData2.size();
   
    }//end for loop
  }//end encodeStringToBits
  //-----------------------------------------------------//
   
}


En principio compila y no da ningun error, pero a la hora de trabajar, me da el siguiente error cuando lo corro:

java.lang.StringIndexOutOfBoundsException: String index out of range: 24
       at javalang.string.substring(String.java:1765)
        .......

Alguna idea de porque se me desborda el substring?
gracias por vuestra ayuda.

Atentamente,
LohanJohan