Just use BinaryCodec from Apache Commons:

 byte[] bytes = new BinaryCodec().toByteArray("1000000111010000");

   

If you want to do such conversion on your own, your code needs some corrections.
You are expecting that A.charAt(i) will return numeric 0 or 1, but will actually return char '0' or '1'. The char data type is a single 16-bit Unicode character with a numeric range from 0 to 2^16, it's values are formally called code points.
To print the code point value you need to cast char to int:

System.out.println("Character " + A.charAt(i) + " has a code point numeric value of " + (int)A.charAt(i));

Output for '0' and '1':

Character 0 has a code point numeric value of 48
Character 1 has a code point numeric value of 49

 

Operator '<<' converts char operands to int, therefore this shifting is producing wrong results because:

firstByte = (byte) (firstByte | (A.charAt(i) << i));

is the same as

firstByte = (byte) (firstByte | ( (int)A.charAt(i) << i));

which for char '0' is the same as shifting value 48 to the left:

firstByte = (byte) (firstByte | ( 48 << i));

 

To convert char '0' or '1' to 0 or 1 numeric value use Character.getNumericValue(A.charAt(i)):

firstByte = (byte) (firstByte | ( Character.getNumericValue(A.charAt(i)) << i));

   

Also shifting by value i is incorrect. You need to shift by (7-i) for the first byte or (7-i%8) for the second byte. When index i reaches 8 it needs to start counting from 0, therefore i%8

   

When printing a values for a byte type you have two options: byte numeric value or binary string representation:

System.out.println("FIRST  byte numeric value = " + xByte[0] + ", binary string representation = " + Integer.toBinaryString((xByte[0]+256)%256));
System.out.println("SECOND byte numeric  value = " + xByte[1] + ", binary string representation = " + Integer.toBinaryString((xByte[1]+256)%256));

output:

FIRST  byte value = -127, binary representation = 10000001
SECOND byte value = -48, binary representation = 11010000

Whole corrected example:

public class ByteTest
{

  public static void main(String[] args)
  {

    byte firstByte=0;
    byte secondByte=0;

    String A = "1000000111010000";
    byte[] xByte = new byte[2];





    for(int i=0 ; i<A.length() ;i++){

      System.out.println("Character " + A.charAt(i) + " has a code point numeric value of " + (int)A.charAt(i));

      if(i<8){
        System.out.println(" i : "+i+" A.char[i] :"+A.charAt(i));
        firstByte = (byte) (firstByte | (Character.getNumericValue(A.charAt(i)) << (7-i)));
      }else{
        System.out.println(" i : "+i+" A.char[i] :"+A.charAt(i));
        secondByte = (byte) (secondByte | (Character.getNumericValue(A.charAt(i)) << (7-i%8)));
      }
    }
    xByte[0] = firstByte;
    xByte[1] = secondByte;


    System.out.println("FIRST  byte numeric value = " + xByte[0] + ", binary string representation = " + Integer.toBinaryString((xByte[0]+256)%256));
    System.out.println("SECOND byte numeric  value = " + xByte[1] + ", binary string representation = " + Integer.toBinaryString((xByte[1]+256)%256));




  }

}
Answer from Sergej Panic on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Byte.html
Byte (Java Platform SE 8 )
July 21, 2026 - Java™ Platform Standard Ed. 8 ... The Byte class wraps a value of primitive type byte in an object.
🌐
DataCamp
datacamp.com › doc › java › byte
byte Keyword in Java: Usage & Examples
The byte keyword in Java is a primitive data type that represents an 8-bit signed two's complement integer. It is used to save memory in large arrays where the memory savings are most needed. The byte data type can store values from -128 to 127.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-integer-bytevalue-method
Java Integer byteValue() Method - GeeksforGeeks
May 6, 2022 - // Java Program to Illustrate byteValue() Method // of Integer Class // Importing required classes import java.lang.*; import java.util.*; // Main Class class GFG { // Main driver method public static void main(String[] args) { // Creating a Byte object Byte b = new Byte("01"); // Converting Byte to byte primitive // using byteValue() method Byte bp = b.byteValue(); // Display statement System.out.println("Byte object : " + b); // Primitive byte value of custom Byte passed above String str = "Primitive byte value of Byte object : " + bp; // Printing byte primitive value System.out.println(str); } }
🌐
Medium
medium.com › @ravirawal13 › bytevalue-method-in-java-13e4a13bba0d
byteValue() method in JAVA
December 29, 2024 - class NumberWithinByteRange { public static void main(String[] args) { Integer integerObject = 13; //Number within byte range byte byteValue = integerObject.byteValue(); System.out.println("Byte value of 13: " + byteValue); } }
🌐
GeeksforGeeks
geeksforgeeks.org › java › byte-bytevalue-method-in-java-with-examples
Byte byteValue() method in Java with examples - GeeksforGeeks
July 11, 2025 - // Java code to demonstrate // Byte byteValue() method class GFG { public static void main(String[] args) { // byte value byte a = 17; // wrapping the byte value // in the wrapper class Byte Byte b = new Byte(a); // byteValue of the Byte Object byte output = b.byteValue(); // printing the output System.out.println("Byte value of " + a + " is : " + output); } }
Top answer
1 of 3
5

Just use BinaryCodec from Apache Commons:

 byte[] bytes = new BinaryCodec().toByteArray("1000000111010000");

   

If you want to do such conversion on your own, your code needs some corrections.
You are expecting that A.charAt(i) will return numeric 0 or 1, but will actually return char '0' or '1'. The char data type is a single 16-bit Unicode character with a numeric range from 0 to 2^16, it's values are formally called code points.
To print the code point value you need to cast char to int:

System.out.println("Character " + A.charAt(i) + " has a code point numeric value of " + (int)A.charAt(i));

Output for '0' and '1':

Character 0 has a code point numeric value of 48
Character 1 has a code point numeric value of 49

 

Operator '<<' converts char operands to int, therefore this shifting is producing wrong results because:

firstByte = (byte) (firstByte | (A.charAt(i) << i));

is the same as

firstByte = (byte) (firstByte | ( (int)A.charAt(i) << i));

which for char '0' is the same as shifting value 48 to the left:

firstByte = (byte) (firstByte | ( 48 << i));

 

To convert char '0' or '1' to 0 or 1 numeric value use Character.getNumericValue(A.charAt(i)):

firstByte = (byte) (firstByte | ( Character.getNumericValue(A.charAt(i)) << i));

   

Also shifting by value i is incorrect. You need to shift by (7-i) for the first byte or (7-i%8) for the second byte. When index i reaches 8 it needs to start counting from 0, therefore i%8

   

When printing a values for a byte type you have two options: byte numeric value or binary string representation:

System.out.println("FIRST  byte numeric value = " + xByte[0] + ", binary string representation = " + Integer.toBinaryString((xByte[0]+256)%256));
System.out.println("SECOND byte numeric  value = " + xByte[1] + ", binary string representation = " + Integer.toBinaryString((xByte[1]+256)%256));

output:

FIRST  byte value = -127, binary representation = 10000001
SECOND byte value = -48, binary representation = 11010000

Whole corrected example:

public class ByteTest
{

  public static void main(String[] args)
  {

    byte firstByte=0;
    byte secondByte=0;

    String A = "1000000111010000";
    byte[] xByte = new byte[2];





    for(int i=0 ; i<A.length() ;i++){

      System.out.println("Character " + A.charAt(i) + " has a code point numeric value of " + (int)A.charAt(i));

      if(i<8){
        System.out.println(" i : "+i+" A.char[i] :"+A.charAt(i));
        firstByte = (byte) (firstByte | (Character.getNumericValue(A.charAt(i)) << (7-i)));
      }else{
        System.out.println(" i : "+i+" A.char[i] :"+A.charAt(i));
        secondByte = (byte) (secondByte | (Character.getNumericValue(A.charAt(i)) << (7-i%8)));
      }
    }
    xByte[0] = firstByte;
    xByte[1] = secondByte;


    System.out.println("FIRST  byte numeric value = " + xByte[0] + ", binary string representation = " + Integer.toBinaryString((xByte[0]+256)%256));
    System.out.println("SECOND byte numeric  value = " + xByte[1] + ", binary string representation = " + Integer.toBinaryString((xByte[1]+256)%256));




  }

}
2 of 3
4

You can't simply cast the A.charAt(i) to an int. It'll return the ASCII code of the 1 and 0.

Therefore, you need to do something like this to get their numeric value:-

int bit = Character.getNumericValue(A.charAt(i)); // This will give the actual value
...
firstByte = (byte) (firstByte | (bit << i));
🌐
CodeGym
codegym.cc › java blog › java types › java byte keyword
Java Byte Keyword
September 28, 2023 - 8 bits (bit is the smallest unit of data containing at most 2 logical states, normally 0 and 1) combine to make a single unit of addressable memory, called a “Byte”. Here’s a theoretical representation of what a byte normally looks like.
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › datatypes.html
Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
A primitive type is predefined by the language and is named by a reserved keyword. Primitive values do not share state with other primitive values. The eight primitive data types supported by the Java programming language are: byte: The byte data type is an 8-bit signed two's complement integer.
Find elsewhere
🌐
Abhi Android
abhiandroid.com › java › byte-examples-program-class.html
Byte With Examples And Program In JAVA | Abhi Android
Default value of Byte is 0. Byte data type is used to save memory in large arrays, mainly in place of integers because byte is four times smaller than an int.
🌐
Tutorialspoint
tutorialspoint.com › java › lang › number_bytevalue.htm
Java - Number byteValue() Method
public class NumberByteVal { public static void main(String[] args) { // Assign an input value to Integer class Integer x = new Integer(134); // Print their value as byte System.out.println("x as integer: " + x + ", x as byte: " + x.byteValue()); } }
🌐
Study.com
study.com › business courses › java programming tutorial & training
Java Data Types: Byte - Lesson | Study.com
October 24, 2019 - A Java byte is the same size as a byte in computer memory: it's 8 bits, and can hold values ranging from -128 to 127.
🌐
TutorialKart
tutorialkart.com › java › java-integer-bytevalue
Java Integer.byteValue() - Get byte value of integer
May 4, 2023 - For example, if integer is 270, its byte value is 0001 0000 1110. As byte can only accommodate eight bits, the last eight bits of the given integer is considered as result.
🌐
W3Schools
w3schools.com › java › ref_keyword_byte.asp
Java byte Keyword
The byte keyword is a data type that can store whole numbers from -128 to 127.
🌐
C# Corner
c-sharpcorner.com › home › technologies › java › byte keyword in java
Byte Keyword in Java
April 21, 2023 - A byte is a keyword used to declare a variable of type byte. The byte keyword is one of the eight primitive data types in Java and represents an 8-bit signed two's complement integer. It can hold values from -128 to 127 inclusive.
🌐
Educative
educative.io › answers › addition-of-byte-values-in-java
Addition of byte values in Java
The byte data type in Java is a signed integer based on the two’s complement 8-bit mechanism. It is different from the int data type that uses 4 bytes (i.e., 32-bit to store a number). The values that can be stored in a single byte are -128 to 127.