我如何将a转换为z成为二进制

时间:2019-01-21 06:21:10

标签: java binary

String a = "a";

String b = "b";
String c = "c";
byte[] bytes = a.getBytes(); b.getBytes(); c.getBytes();

StringBuilder binary = new StringBuilder();

for (byte byt : bytes) {
    int val = byt;

    for (int i = 0; i < 8; i++) {
        binary.append((val & 128) == 0 ? 0 : 1);
        val <<= 1;
    }
    binary.append(' ');
}

Displaytext.setText(UserInput.getText() + " = " + binary);

1 个答案:

答案 0 :(得分:0)

您只需要使用用户输入来初始化输入数组:

byte[] bytes = UserInput.getText().getBytes();