Issues with reversing bit shifts that roll over the maximum byte size?
- by Terri
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?