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);
1
  • Try this: String codest="11010101"; int edcode=Integer.parseInt(codest, 2); Commented Mar 19, 2014 at 9:42

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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

4 Comments

thank you :) i would be grateful if you explain the logic behind it
JRE needs to know about the base of your number.
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.docs.oracle.com/javase/7/docs/api/java/lang/…, int)
Can you please accept the answer. You may have to click right just below the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.