2

I'm trying to simulate a Hamming-Code 4,7 which is working properly. I'm working with boolean arrays insted of really bits to simplify it a bit logically. I input some Characters, there converted to ASCII and the Simulation goes on. After all I end up with an BooleanArray of length numberofcharacters*8 where every [] equals one bit. This method should just reconvert this back to ascii code and it work semiproperly. If the ASCII number is even, erverything is fine, but when it's odd the last bit it always 'forgotten'. Thanks for your help

Dear Simon

private static String booleantoString(boolean[] bool){ 
    int[] array = new int[bool.length/8];
    for(int a=0;a<bool.length/8;a++){
        for(int n = a*8; n<(a*8+7); n++){   
            if(bool[n] == true){
                array[a] = setBit(array[a],0);
            }
            array[a] <<= 1;
            }

        System.out.print("ASCII-Code: " + array[a]+ " ");
        System.out.print("Zeichen: " +(char)array[a] + " ");
    }
    return s;   
 }
KevinDTimm
  • 14,226
  • 3
  • 42
  • 60

1 Answers1

0

I haven't walked it thoroughly, but I think it's an off by one error due to integer division round down. The loop condition bool.length/8 specifically seems wrong. If bool.length is 0-7 then bool.length/8 is 0. Likewise for any bool.length from 8-15 then bool.length/8 is 1. So any incomplete bytes trailing off your array will always be ignored. Maybe try changing it to (bool.length/8)+1.

Trevor Brown
  • 169
  • 3