-3

I have binary like 1001001 100000 1110011 1100001 1101001 1100100 100000 1100110 1110101 1100011 1101011 100000 1000111 1000001 1001110 1000101 1010011 1001000 I am trying to converted them to String. There's some source code I have tried from this link None are working. I am getting the same error everytime.

public static String binaryToText(String binaryString) {
    StringBuilder stringBuilder = new StringBuilder();
    int charCode;
    for (int i = 0; i < binaryString.length(); i += 8) {
        charCode = Integer.parseInt(binaryString.substring(i, i + 8), 2);
        String returnChar = Character.toString((char) charCode);
        stringBuilder.append(returnChar);
    }
    return stringBuilder.toString();
}

Here's the error

Process: com.binary, PID: 4784
java.lang.NumberFormatException: Invalid int: "1111001 "
    at java.lang.Integer.invalidInt(Integer.java:138)
    at java.lang.Integer.parse(Integer.java:410)
    at java.lang.Integer.parseInt(Integer.java:367)
    at com.binary.MainActivity.binaryToText(MainActivity.java:97)
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
kazi Abid
  • 21
  • 6
  • 2
    The groups are made of 7 digits, you're reading 8 at a time (when substringing). In general, don't use code you don't understand. – Federico klez Culloca Jan 07 '21 at 09:40
  • @FedericoklezCulloca I changed the code. `for (int i = 0; i < binaryString.length(); i += 7) {` I changed every 8 to 7. But, I am getting another error now. `java.lang.StringIndexOutOfBoundsException: length=8; regionStart=7; regionLength=7 at java.lang.String.startEndAndLength(String.java:504) at java.lang.String.substring(String.java:1333) at com.binary.MainActivity.binaryToText(MainActivity.java:97)` – kazi Abid Jan 07 '21 at 09:45
  • Re-read my comment, please (you probably didn't notice my edit, my fault for not making it clear from the beginning). And again, try to understand the code you're using instead of changing random bits until it works. – Federico klez Culloca Jan 07 '21 at 10:01
  • @FedericoklezCulloca Ow! it won't work. I understood – kazi Abid Jan 07 '21 at 10:05
  • @FedericoklezCulloca So, can you say How can I decode it? – kazi Abid Jan 07 '21 at 10:09
  • the exception says that the "blank" at the end of the binary string is invalid:"1111001 " Use trim() before do the converting and that will work. –  Jan 07 '21 at 10:11
  • @Joe Although having error. But, this time I am getting another type of error `java.lang.StringIndexOutOfBoundsException: length=7; regionStart=0; regionLength=8` – kazi Abid Jan 07 '21 at 10:20
  • that is another problem. Nothing to do with the NumberFormatException, but it's the indexing problem in your loop. –  Jan 07 '21 at 10:23

1 Answers1

0

The problem is that those group of bits are not all the same length. So do yourself a favor and, instead of counting steps just split the string on " " and handle each bit group on its own:

public static String binaryToText(String binaryString) {
    StringBuilder stringBuilder = new StringBuilder();
    int charCode;
    // Split the string
    String[] parts = binaryString.split(" ");
    // Consider each part on its own
    for (String part : parts) {
        charCode = Integer.parseInt(part, 2);
        String returnChar = Character.toString((char) charCode);
        stringBuilder.append(returnChar);
    }
    return stringBuilder.toString();
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • 1
    As a side note, the text resulting from the string in the question can be considered blasphemous by some, so run this code on that input at your own discretion. – Federico klez Culloca Jan 07 '21 at 10:19