Ian
Ian

Reputation: 3898

How do I convert a character into binary?

How would you convert a given character in a string to a its corresponding binary value?

public void send(DataFrame frame) {
    String bitString = frame.toString();
        for (int a = 0; a < bitString.length(); a++) {
            char c = bitString.charAt(a);
            ????
    }
}

where frame is defined to be a byte array (as byte [] in the DataFrame class

Upvotes: 4

Views: 20064

Answers (2)

Dmytro Plekhotkin
Dmytro Plekhotkin

Reputation: 1993

String binaryString = Integer.toBinaryString(0x100 + bytes[i]).substring(2);

Upvotes: 3

subash
subash

Reputation: 3140

try this

        String bitString = frame.toString();
            for (int a = 0; a < bitString.length(); a++) {
                byte[] b = new byte[1024];
                b = bitString.getBytes();
                System.out.println(Arrays.toString(b));
        }

Upvotes: 0

Related Questions