Queries and Questions

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=newString(codear);int edcode=Integer.parseInt(nc);
 

 


0
down voteunaccept

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

thank you 🙂 i would be grateful if you explain the logic behind it –  Manika Mar 19 at 9:46
JRE needs to know about the base of your number. –  Varun Mar 19 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.docs.oracle.com/javase/7/docs/api/java/lang/…, int) –  Varun Mar 19 at 10:18
Thanks @varun 🙂 –  Manika Mar 19 at 10:27
Can you please accept the answer. You may have to click right just below the answer. –  Varun Mar 19 at 10:28
Advertisements

Constructive and genuine appreciation and/or criticism most welcome

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s