Issues with reversing bit shifts that roll over the maximum byte size?

Posted by Terri on Stack Overflow See other posts from Stack Overflow or by Terri
Published on 2010-04-09T17:09:11Z Indexed on 2010/04/09 17:13 UTC
Read the original article Hit count: 328

Filed under:
|

I have a string of binary numbers that was originally a regular string and will return to a regular string after some bit manipulation.

I'm trying to do a simple caesarian shift on the binary string, and it needs to be reversable. I've done this with this method..

public static String cShift(String ptxt, int addFactor)
    {
        String ascii = "";
        for (int i = 0; i < ptxt.length(); i+=8)
        {
            int character = Integer.parseInt(ptxt.substring(i, i+8), 2);
            byte sum = (byte) (character + addFactor);
            ascii += (char)sum;
        }
        String returnToBinary = convertToBinary(ascii);
        return returnToBinary;
    }

This works fine in some cases. However, I think when it rolls over being representable by one byte it's irreversable. On the test string "test!22*F ", with an addFactor of 12, the string becomes irreversible. Why is that and how can I stop it?

© Stack Overflow or respective owner

Related posts about java

Related posts about conversion