How can i change the base 2 to base 10? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How can i change the base 2 to base 10?

for example the input is:11001 and the output is:25 please guide me with c++

22nd Dec 2017, 11:29 PM
Sina Etm
Sina Etm - avatar
3 Answers
+ 25
http://www.cplusplus.com/forum/beginner/181908/ Base-10 to Base-2 Conversion - C++ Forum - Cplusplus.com
22nd Dec 2017, 11:55 PM
Nithiwat
Nithiwat - avatar
+ 1
Basically all numbers is an array read left to right: e.g. in int 1031 = 1x10^3 + 0x10^2 + 3x10^1 + 1x10^0 To get the individual digits: (1031/10^3)%10,(1031/10^2)%10,(1031/10^1)%10, (1031/10^0)%10 Same is true for binary: e.g. 11001 = 1x2^4 + 1x2^3 + 0x2^2 + 0x2^1 + 1x2^0 Then you need to convert whatever number you have to a string. Pseudo code on string N: X base to base 10: Base =X; Num=0; Tmp=0; Tmpchar=‘0’; For I=0; I < N.length; I++ { Num=Num*X Tmp = int (N[i]-‘0’) // doesn’t work past base 10; Num=Num+Tmp } Base 10 # to baseX string: Num String s =“”; TmpNum While Num>0{ TmpNum=Num% X Num = Num / X s = TmpNum+s; } Rough implementation with outside references: https://code.sololearn.com/cqtlOGCgM7U0/?ref=app
23rd Dec 2017, 3:28 AM
H Chiang
0
My code got down voted, so any opinion as to why? And how do you feel about downvoting? https://www.sololearn.com/Discuss/857886/?ref=app
24th Dec 2017, 2:37 AM
H Chiang