[Python Code]Python đổi nhị phân sang thập phân

Python đổi nhị phân sang thập phân

Cách lấy một giá trị thập phân từ một dãy số nhị phân với ngôn ngữ lập trình python.

#phattrienphanmem123az.com
import math
import os


def BinaryToDecimal(bVal):
    dVal, i = 0, 0
    while bVal != 0:
        dec = bVal % 10
        dVal = dVal + dec * pow(2, i)
        bVal = bVal // 10
        i += 1
    return dVal


def main():
    dec = BinaryToDecimal(1111)
    return 0


if __name__ == "__main__":
    main()

 

Ouput: dec = 15

 

Ok.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.