0

I first converted a char to an int and then the int to a binary string. Then I tried to convert this int back to a char but I got an error.

I tried this:

char myasciireturn =  (char) intname;

And I got the following error:

Inconvertable types; cant cast 'java.lang.Integer' to 'char'

Here is my code:

Integer tester = Integer.valueOf("01000001", 2);
nextstep = Character.toString((char) tester);

Thanks for your help in advance,
Max

Bubletan
  • 3,833
  • 6
  • 25
  • 33
ogris
  • 21
  • 5
  • Possible duplicate of [Java - char, int conversions](http://stackoverflow.com/questions/21317631/java-char-int-conversions) – Bruno_Ferreira Dec 09 '16 at 23:26
  • converting int to char and converting Integer to char are different. You can use `int tester = Integer.valueOf("01000001", 2);`, that shouldn't give you problem when converting the `int tester` to char later on – Anthony C Dec 09 '16 at 23:31

1 Answers1

0

I believe that the problem is the size of the Integer. Where tester is greater than eight bits, there are no characters to cast that number to. I would do this instead: First, convert the binary number back to a decimal number. (For help with that go to this link: http://beginnersbook.com/2014/07/java-program-for-binary-to-decimal-conversion/) After the number is decimal, this is how to convert an int to a char:

int tester = 132; //Or whatever the decimal number is
char letter = (char)tester;

I hope that this is useful.

Josh Heaps
  • 335
  • 1
  • 8