when you use the String constructor (byte []), it does not necessarily take one letter for each byte, it takes the default encoding; if this is, say, UTF-8, then the constructor will try to decode some characters from two or three bytes, and not just from one.
Since you use bit padding to convert byte to byte, the result may be different if you use the default encoding.
If you use only ASCII characters, you can try this version of your function:
// ONLY if you use ASCII as Charset public static String convert(String s) { Charset ASCII = Charset.forName("ASCII"); byte[] bytes = s.getBytes(ASCII); byte[] convert = new byte[bytes.length]; for (int i = 0; i < bytes.length; i++) { convert[i] = (byte) (~bytes[i] & 0x7F); } return new String(convert, ASCII); }
source share