python - Binary to Decimal converter works backwards -
i have created basic program converting 8-bit binary decimal, when run program, works backwards, e.g. if enter binary number '00000001'
program return 128
. here code:
binaryvalue=input("please enter 8 bit binary number.") binaryvalue=str(binaryvalue) if len(binaryvalue) <= 8: print("your number accurate.") value_index=0 total=0 print("your number valid.") in range(len(binaryvalue)): total = total + (int(binaryvalue[value_index])) * 1 * (2**(value_index)) value_index = value_index+1 print("your decimal number is: "+str(total))
so, mentioned jonrsharpe , moe
reverse input:
binaryvalue = str(binaryvalue)[::-1]
or, can place offset in power:
total = total + (int(binaryvalue[value_index])) * 1 * (2**(len(binaryvalue)-value_index-1))
both doing same thing though.
Comments
Post a Comment