-7

I have a text file, which is divided in 3 part, and now I want to convert this divided block to binary format and store in database. Please help me to solve this problem.

Thanks.

String firstblock = “If debugging is thae process of removing software bugs, then programming must be the process of putting them in. Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program.”;

and I need firstblock in binary form.

3 Answers3

0

Use getBytes() method.

See the below

String text = "Hello World!";
byte[] bytes = text.getBytes(StandardCharsets.UTF_8);

Update:

Try with below code:

String s = "foo";
  byte[] bytes = s.getBytes();
  StringBuilder binary = new StringBuilder();
  for (byte b : bytes)
  {
     int val = b;
     for (int i = 0; i < 8; i++)
     {
        binary.append((val & 128) == 0 ? 0 : 1);
        val <<= 1;
     }
     binary.append(' ');
  }
  System.out.println("'" + s + "' to binary: " + binary);

Reference :

Convert A String (like testing123) To Binary In Java

Community
  • 1
  • 1
0
 String firstblock = “If debugging is thae process of removing software bugs, then programming must be the process of putting them in. Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program.”;
  byte[] bytes = firstblock.getBytes();
  StringBuilder binary = new StringBuilder();
  for (byte b : bytes)
  {
     int val = b;
     for (int i = 0; i < 8; i++)
     {
        binary.append((val & 128) == 0 ? 0 : 1);
        val <<= 1;
     }
     binary.append(' ');
  }
  System.out.println(binary);

and you can check it binary to string

Mahesh Shukla
  • 186
  • 10
0
String s = "foo";
  byte[] bytes = s.getBytes();
  StringBuilder binary = new StringBuilder();
  for (byte b : bytes)
  {
     int val = b;
     for (int i = 0; i < 8; i++)
     {
        binary.append((val & 128) == 0 ? 0 : 1);
        val <<= 1;
     }
     binary.append(' ');
  }
  System.out.println("'" + s + "' to binary: " + binary);

you try with this code if you want to use online tool

An English to binary translator is a tool that can convert text written in the English language into a sequence of 0s and 1s, which is known as binary code. This is the language that computers understand and use to process and store data.

Here is an example of how it would work:

Input: "Hello" Output: "01001000 01100101 01101100 01101100 01101111"

The translator would take the word "Hello" and convert each letter into its corresponding 8-bit binary code representation.

bca tech
  • 26
  • 1
  • 8