-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.

2 Answers2

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
  • 187
  • 10