1

Input in is converted correctly. output for in is gd`x]jef But in1 is not getting output. because in1 is the binary string of UTF-8 text of ���GF

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

public class Tostr {


public void toStrng(String input) throws UnsupportedEncodingException
{
    //String input = "1111010 1011001 1100010"; // Binary input as String

   // byte[] code = input.getBytes("UTF-8");
   // byte ptext[] = input.getBytes(Charset.forName("UTF-8"));
  //  String in = new String(ptext, Charset.forName("UTF-8"));

    StringBuilder sb = new StringBuilder(); // Some place to store the chars

    Arrays.stream( input.split(" ")).forEach(s -> sb.append((char) Integer.parseInt(s, 2)));


    String output = sb.toString();
    System.out.println(output);

    //return output;
}

public static void main(String arg[]) throws UnsupportedEncodingException
{
    String in="1100111 1100100 1100000 1111000 1011101 1101010 1100101 1100110";
    String in1="11111111111111111111111111101111 11111111111111111111111110111111 11111111111111111111111110111101 101 10101 11111111111111111111111111101111 11111111111111111111111110111111 11111111111111111111111110111101 11111111111111111111111111101111 11111111111111111111111110111111 11111111111111111111111110111101 1000111 1000110 101";
    Tostr ts= new Tostr();
    ts.toStrng(in);        
    ts.toStrng(in1);

}     
}

I need to get the output on in1 as ���GF please help me to correct the code the exception occurs is NumberFormatException.forInputString

  • 2
    Possible duplicate of [How to convert a Binary String to UTF-8 String in java?](https://stackoverflow.com/questions/41909888/how-to-convert-a-binary-string-to-utf-8-string-in-java) – yash Oct 05 '18 at 07:54
  • There isn't anything built in to do that because basically no code actually expects the characters `'0'` and `'1'`. – Louis Wasserman Oct 05 '18 at 08:09

0 Answers0