Вот решение уровня предприятия для вашей работы уровня предприятия:
public static void main(String[] args) {
if (args.length == 0 || args[0] == null || (args[0] = args[0].trim()).isEmpty()) {
System.out.println("No argument passed or argument empty!");
return;
}
String arg = args[0];
System.out.println("arg: " + arg + ", arg len: " + arg.length());
BitSet bs = new BitSet(arg.length());
for (int i = 0; i < arg.length(); i++) {
if (arg.charAt(i) == '1') {
bs.set(i, true);
}
}
ByteBuffer bb = ByteBuffer.wrap(bs.toByteArray());
Charset cs = Charset.forName("UTF-8");
CharsetDecoder csd =
cs.newDecoder().onMalformedInput(CodingErrorAction.REPORT).
onUnmappableCharacter(CodingErrorAction.REPORT)
;
try {
CharBuffer cb = csd.decode(bb);
String uns = cb.toString();
System.out.println("Got unicode string of len " + uns.length() + ": " + uns + " from " + arg + " -- no errors!");
} catch (CharacterCodingException cce) {
System.out.println("Invalid UTF-8 unicode string! " + cce.getMessage());
}
}
Проверка:
public static void test() {
StringBuilder sb = new StringBuilder();
byte[] byt = new String("stupid interview").getBytes();
BitSet byt1 = fromByteArray(byt);
for (int i = 0; i < byt1.size(); i++) {
sb.append(byt1.get(i) ? "1" : "0");
}
String[] st = new String[1];
st[0] = sb.toString();
main(st);
}
public static BitSet fromByteArray(byte[] bytes) {
BitSet bits = new BitSet();
for (int i=0; i<bytes.length*8; i++) {
if ((bytes[bytes.length-i/8-1]&(1<<(i%8))) > 0) {
bits.set(i);
}
}
return bits;
}
Выход:
11001110001011101010111000001110100101100010011000000100100101100111011000101110101001100100111001101110100101101010011011101110
arg: 11001110001011101010111000001110100101100010011000000100100101100111011000101110101001100100111001101110100101101010011011101110, arg len: 128
{0, 1, 4, 5, 6, 10, 12, 13, 14, 16, 18, 20, 21, 22, 28, 29, 30, 32, 35, 37, 38, 42, 45, 46, 53, 56, 59, 61, 62, 65, 66, 67, 69, 70, 74, 76, 77, 78, 80, 82, 85, 86, 89, 92, 93, 94, 97, 98, 100, 101, 102, 104, 107, 109, 110, 112, 114, 117, 118, 120, 121, 122, 124, 125, 126}
Got unicode string of len 16: stupid interview from 11001110001011101010111000001110100101100010011000000100100101100111011000101110101001100100111001101110100101101010011011101110 -- no errors!