How I can in one method 《public void 》convert binary to decimal easy way in Java for beginners? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How I can in one method 《public void 》convert binary to decimal easy way in Java for beginners?

Hi, i've started learn Java and have to write a Converter machine, which should convert binary to decimal etc. , but I haven't any idea what to do. Will appreciate all of your ideas ☺

16th Jan 2020, 7:49 PM
Mika Timur
Mika Timur - avatar
1 Answer
0
Take input a binary value for example 10101 Equalent decimal is =2^0*1+2^1*0+2^2*1+2^3*0+2^4*1 =1*1+2*0+4*1+8*0+16*1 =1+0+4+0+16 =21(decimal equalent) Here, 2^0*1 is 2 power 0*(0th position bit 1) So if 0th bit's corresponding value it obtains... By a for or while loop you can done it from right most bit to left most bit.. Num%10 obtains right most bit. Next number is num=num/10... In this way you can generate a converter.. Hoping this helps you if you need to generate your own converter other than predefined one... Edit: see this https://www.sololearn.com/learn/4401/?ref=app
16th Jan 2020, 8:44 PM
Jayakrishna 🇮🇳