0

i want to get the integer i.e decimal number corresponding to the binary string nc. However this does not happen despite using parseInt(). for eg if nc="11000101" then edcode is also having the same value instead of giving me the decimal representation of nc. Can anyone please help

String codest="11010101";
char[] codear=codest.toCharArray();
codear[4]=codear[5];
String nc= new String(codear);
int edcode=Integer.parseInt(nc);
Manika
  • 29
  • 1
  • 10

2 Answers2

1

parseInt(String s, int radix) Parses the string argument as a signed integer in the radix specified by the second argument.

From here.

Try this:

int edcode=Integer.parseInt(nc, 2);
user2424380
  • 1,393
  • 3
  • 16
  • 29
0

int edcode=Integer.parseInt(nc, 2); This will work

Varun
  • 583
  • 5
  • 12
  • thank you :) i would be grateful if you explain the logic behind it – Manika Mar 19 '14 at 09:46
  • JRE needs to know about the base of your number. – Varun Mar 19 '14 at 10:12
  • JRE needs to know about the base of your number. If number is in binary, we need to tell JRE that number is in binary format. For the same reason we passed 2 as binary is base 2. I hope this explains well. Please refer this for more details.http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String, int) – Varun Mar 19 '14 at 10:18
  • Can you please accept the answer. You may have to click right just below the answer. – Varun Mar 19 '14 at 10:28