String (String packet) { StringBuilder buffer = new StringBuilder(packet.length()); for (int i = 0; i < packet.length(); i++) { char chr = packet.charAt(i); if (chr == ourSpecialSymbol) { buffer.append(chr); buffer.append(chr); continue; } boolean appendChar = true; for (char c : ourSymbolsToEncode) { if (c == chr) { buffer.append(ourSpecialSymbol); final String code = String.valueOf((int)chr); for (int count = CODE_LENGTH - code.length(); count > 0; count--) { buffer.append("0"); } buffer.append(code); appendChar = false; break; } } if (appendChar) { buffer.append(chr); } } return buffer.toString(); }