SamSmith SamSmith - 2 months ago 16
Java Question

Convert binary string to text string

First, please do not mark as duplicate because I know the other SO post does not solve my issue..thank you.

I have a

String
variable called
M
which contains the binary value:

0001101 0010110 0011101 0001110 0010100 0010101 0010001 0001000 0000110 0000110 0001100 0001101 0011010 0011100 0010101 0001110 0011010 0011110 0001100 0000110 0011000 0001011 0001101 0000111 0011110 0001010 0001000 0010001 0000111 0011100 0001100 0001001 0010001 0001000 0010010 0001100 0001100 0011001 0010111 0001100 0010111 0000111 0001011 0010110 0010011 0011110 0010000 0010001 0010110 0010011 0011110 0001011 0010110 0000110 0001101 0011010 0011101 0001111 0000111 0000111 0011010 0011100 0001101 0011100 0011001 0011001 0011001 0011101 0011010 0010001 0010011 0010001 0011110 0010111 0001111 0001001 0010101 0011011 0001110 0001011 0011110 0011101 0000110 0011000 0000110 0010101 0001100 0001110 0010100 0011001 0011000 0001100 0001100 0001000 0011000 0001111 0001001 0011100 0011010 0011110 0010110 0011001 0011101 0001110 0010101 0011010 0000111 0010101 0010011 0000101 0010010 0001011 0011110 0001110 0011101 0010001 0001010 0011010 0010110 0011011 0011001 0011000 0010000 0010111 0001100 0010101 0001000 0000101 0000101 0011101 0001100 0010001 0011001 0001110 0001000 0001100 0010101 0001011 0001000 0010000 0001000 0001111 0010101 0001100 0001010 0001111 0011010 0010010 0010010 0011100 0011001 0011001 0010111 0001100 0010010 0000111 0000110 0011001 0001011 0011011 0001101 0011000 0011001 0010000 0000101 0000111 0010111 0000101 0011101 0000111 0010101 0011110 0001101 0011001 0001110 0011100 0001100 0011010 0010010 0011101 0000110 0001111 0011101 0010111 0010110 0011000 0010000 0001000 0001001 0011110 0010111 0001110 0000101 0001010 0001011 0001011 0011001 0011010 0010110 0010101 0001010 0001111 0001100 0011110 0001010 0010011 0011000 0000111 0010011 0001000 0010011 0011110 0011100 0001010 0011000 0011100 0011100 0010110 0011100 0010000 0011110 0001000 0011000 0000110 0001100 0001010 0011010 0011010 0000110 0010000 0001111 0010110 0001010 0010010 0010100 0011101 0011101 0011010 0001101 0011100 0001000 0011011 0010001 0010101 0010000 0010000 0010111 0001001 0011011 0011100 0001100 0010110 0001110 0001100 0011101 0011011


The appropriate text equivalent is:

ribqkjnwyysrecjqeasygtrxauwnxcsvnwmssfhshxtilaonilatiyrebpxxecrcfffbenlnahpvjdqtabygyjsqkfgsswgpvceaifbqjexjlzmtaqbnueidfgohsjwzzbsnfqwsjtwowpjsupemmcffhsmxyftdrgfozxhzbxjarfqcsembypbhigowvahqzuttfeijupsaulgxlwlacugccicoawgysueeyopiumkbbercwdnjoohvdcsiqsbd


I am trying to convert the
String
variable M from binary to text. Based upon what I understand, and read on other SO posts, I coded the following:

int charCode = Integer.parseInt(M.trim(), 2);
String str = new Character((char)charCode).toString();


where
M
is a
String
containing the binary value that I discussed above before, and
str
should be the appropriate text equivalent that you see above.

However, I am receiving the following error:

Exception in thread "main" java.lang.NumberFormatException: For input string: "0001101 0010110 0011101 0001110 0010100 0010101 0010001 0001000 0000110 0000110 0001100 0001101 0011010 0011100 0010101 0001110 0011010 0011110 0001100 0000110 0011000 0001011 0001101 0000111 0011110 0001010 0001000 0010001 0000111 0011100 0001100 0001001 0010001 0001000 0010010 0001100 0001100 0011001 0010111 0001100 0010111 0000111 0001011 0010110 0010011 0011110 0010000 0010001 0010110 0010011 0011110 0001011 0010110 0000110 0001101 0011010 0011101 0001111 0000111 0000111 0011010 0011100 0001101 0011100 0011001 0011001 0011001 0011101 0011010 0010001 0010011 0010001 0011110 0010111 0001111 0001001 0010101 0011011 0001110 0001011 0011110 0011101 0000110 0011000 0000110 0010101 0001100 0001110 0010100 0011001 0011000 0001100 0001100 0001000 0011000 0001111 0001001 0011100 0011010 0011110 0010110 0011001 0011101 0001110 0010101 0011010 0000111 0010101 0010011 0000101 0010010 0001011 0011110 0001110 0011101 0010001 0001010 0011010 0010110 0011011 0011001 0011000 0010000 0010111 0001100 0010101 0001000 0000101 0000101 0011101 0001100 0010001 0011001 0001110 0001000 0001100 0010101 0001011 0001000 0010000 0001000 0001111 0010101 0001100 0001010 0001111 0011010 0010010 0010010 0011100 0011001 0011001 0010111 0001100 0010010 0000111 0000110 0011001 0001011 0011011 0001101 0011000 0011001 0010000 0000101 0000111 0010111 0000101 0011101 0000111 0010101 0011110 0001101 0011001 0001110 0011100 0001100 0011010 0010010 0011101 0000110 0001111 0011101 0010111 0010110 0011000 0010000 0001000 0001001 0011110 0010111 0001110 0000101 0001010 0001011 0001011 0011001 0011010 0010110 0010101 0001010 0001111 0001100 0011110 0001010 0010011 0011000 0000111 0010011 0001000 0010011 0011110 0011100 0001010 0011000 0011100 0011100 0010110 0011100 0010000 0011110 0001000 0011000 0000110 0001100 0001010 0011010 0011010 0000110 0010000 0001111 0010110 0001010 0010010 0010100 0011101 0011101 0011010 0001101 0011100 0001000 0011011 0010001 0010101 0010000 0010000 0010111 0001001 0011011 0011100 0001100 0010110 0001110 0001100 0011101 0011011"


I researched this error, and then added the
trim
method thinking it would remove any trailing spaces, but that did not solve the error.

Is the
String
value too large? Any suggestions as to why and how to correct this issue are appreciated.

UPDATE:
Additionally details about error:

at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at CaesarCipher.decrypt(C.java:194)


*Line 194 is:

int charCode = Integer.parseInt(M.trim(), 2);


UPDATE #2: (I changed
result
to
result1
)
Given,

String result1 = "";
for(String binNum: M.trim().split(" ")){
int charCode = Integer.parseInt(binNum, 2);
result1+=Character.toString(charCode);
}
System.out.println(result1);


The error is:

error: incompatible types: possible lossy conversion from int to char
result1+=Character.toString(charCode);


UPDATE #3:

Error corrected, but I am receiving the string:

mv}ntuqhfflmz|unz~lfxkmg~jhqg|liqhrllywlwgkvs~pqvs~kvfmz}oggz|m|yyy}zqsq~woiu{nk~}fxfulntyxllhxoi|z~vy}nuzguserk~n}qjzv{yxpwluhee}lqynhlukhphouljozrr|yywlrgfyk{mxypegwe}gu~myn|lzr}fo}wvxphi~wnejkkyzvujol~jsxgshs~|jx||v|p~hxfljzzfpovjrt}}zm|h{quppwi{|lvnl}{


when I should be receiving the string:

ribqkjnwyysrecjqeasygtrxauwnxcsvnwmssfhshxtilaonilatiyrebpxxecrcfffbenlnahpvjdqtabygyjsqkfgsswgpvceaifbqjexjlzmtaqbnueidfgohsjwzzbsnfqwsjtwowpjsupemmcffhsmxyftdrgfozxhzbxjarfqcsembypbhigowvahqzuttfeijupsaulgxlwlacugccicoawgysueeyopiumkbbercwdnjoohvdcsiqsbd


UPDATE #4:

When removing 96, I receive the following:

enter image description here
What is this?

Answer

Your problem is it doesn't know what to do with the spaces between the binary numbers, and also that the whole string is not itself one number but a grouping of a bunch of numbers. The solution is to parse each individual number as a character and then collect the result.

You'll need to do something like this:

String result = "";
for(String binNum: M.trim().split(" ")){
     int charCode = Integer.parseInt(binNum, 2);
     result+=Character.toString((char)charCode);
}
System.out.println(result); // Here is the complete string...