Hướng dẫn convert number to 8 bit binary python - chuyển đổi số thành python nhị phân 8 bit

Tôi hiện đang tạo một chương trình Python có liên quan đến byte, nó sẽ nhắc người dùng nhập 2 số nguyên, thêm chúng lại với nhau và nếu nó nằm trong phạm vi kích thước thực tế của một byte (0 đến 255) hiển thị số đó và cũng cho Giá trị tương ứng ở dạng nhị phân 8 bit của nó, ví dụ, tính toán bằng 1, hiển thị số nhị phân mà Python tuyên bố là chính xác.

Đây là nhiệm vụ mà tôi đang làm ~

"Chương trình phải kiểm tra dữ liệu đầu vào cho loại dữ liệu được phép và giới hạn giá trị dữ liệu (giá trị của số nguyên không được vượt quá kích thước thực tế của các số nguyên được mã hóa byte, tức là Min 00000000 và tối đa 11111111 trong cơ sở 2 hoặc tối thiểu 0 và tối đa 255 trong cơ sở 10 cho các số nguyên dương "đầu vào ở cả định dạng nhị phân và thập phân với chuyển đổi sang nhị phân

def add():
    Num1 = int(input("Input the first number between 0 and 255, the calculated answer must not be above 255: "))
    Num2 = int(input("Input the second number between 0 and 255, the calculated answer must not be above 255: "))

    calculatedanswer = Num1+Num2

    if calculatedanswer >= 0 and calculatedanswer <=255:
        print("The answer in decimal is" ,calculatedanswer,)

        **bin(calculatedanswer)**

    elif calculatedanswer < 0 and calculatedanswer >255:
        print("Please ensure the added numbers are above 0 and below 255")
        add()

Đây là mã của tôi cho đến nay, tôi không gặp khó khăn gì khi hiển thị số thập phân tiêu chuẩn, nhưng tôi thực sự không thể có được bin(calculatedanswer) để hiển thị tương đương nhị phân của nó. Tôi đã thử sử dụng phương pháp này mà tôi tìm thấy trên YouTube.

Tôi nghĩ rằng vấn đề chính của tôi ở đây là sự thiếu hiểu biết của tôi về cách "bin" hoạt động trên Python vì đây thực sự là lần đầu tiên tôi sử dụng nó.

Tôi đã đặt dấu hoa thị xung quanh dòng mà tôi đang gặp rắc rối.

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọc
    Examples : 

    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010

    Bàn luận Recursive solution

    DecimalToBinary(num):
            if num >= 1:
                DecimalToBinary(num // 2)
               print num % 2 

    Hướng dẫn convert number to 8 bit binary python - chuyển đổi số thành python nhị phân 8 bit

    Được đưa ra một số thập phân làm đầu vào, nhiệm vụ là viết một chương trình Python để chuyển đổi số thập phân đã cho thành số nhị phân tương đương. Các mẫu mực: & nbsp;

    Python3

    Phương pháp số 1: Giải pháp đệ quy

    Dưới đây là việc thực hiện giải pháp đệ quy ở trên: & NBSP;

    def DecimalToBinary(num):

        if

    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    0
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    1
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    2
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    3

    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    4
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    5
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    6
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    6
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    8
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    9

        

    DecimalToBinary(num):
            if num >= 1:
                DecimalToBinary(num // 2)
               print num % 2 
    1
    DecimalToBinary(num):
            if num >= 1:
                DecimalToBinary(num // 2)
               print num % 2 
    2223
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    8
    DecimalToBinary(num):
            if num >= 1:
                DecimalToBinary(num // 2)
               print num % 2 
    5
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    1
    DecimalToBinary(num):
            if num >= 1:
                DecimalToBinary(num // 2)
               print num % 2 
    7

        

    1001010110001
    1010
    101011001
    9

    if

    DecimalToBinary(num):
            if num >= 1:
                DecimalToBinary(num // 2)
               print num % 2 
    9
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    1
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    1
    1001010110001
    1010
    101011001
    2
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    3
    Decimal to binary using in-built function 

    Python3

        

    1001010110001
    1010
    101011001
    5
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    1
    1001010110001
    1010
    101011001
    7

    Phương pháp số 2: thập phân đến nhị phân bằng cách sử dụng hàm được xây dựng & nbsp;

    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    4
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    5
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    6
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    6
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    8
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    9

        

    DecimalToBinary(num):
            if num >= 1:
                DecimalToBinary(num // 2)
               print num % 2 
    1bin(calculatedanswer)6bin(calculatedanswer)7bin(calculatedanswer)8

        

    DecimalToBinary(num):
            if num >= 1:
                DecimalToBinary(num // 2)
               print num % 2 
    1bin(calculatedanswer)6def2bin(calculatedanswer)8

        

    DecimalToBinary(num):
            if num >= 1:
                DecimalToBinary(num // 2)
               print num % 2 
    1
    DecimalToBinary(num):
            if num >= 1:
                DecimalToBinary(num // 2)
               print num % 2 
    2223
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    8
    DecimalToBinary(num):
            if num >= 1:
                DecimalToBinary(num // 2)
               print num % 2 
    5
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    1
    DecimalToBinary(num):
            if num >= 1:
                DecimalToBinary(num // 2)
               print num % 2 
    7

    if

    DecimalToBinary(num):
            if num >= 1:
                DecimalToBinary(num // 2)
               print num % 2 
    9
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    1
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    1
    1001010110001
    1010
    101011001
    2
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    3
    Without in-built function

    Python3

        

    1001010110001
    1010
    101011001
    5
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    1
    1001010110001
    1010
    101011001
    7

    Phương pháp số 2: thập phân đến nhị phân bằng cách sử dụng hàm được xây dựng & nbsp;

    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    4
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    5
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    6
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    6
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    8
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    9

        

    DecimalToBinary(num):
            if num >= 1:
                DecimalToBinary(num // 2)
               print num % 2 
    1bin(calculatedanswer)6bin(calculatedanswer)7bin(calculatedanswer)8

        

    DecimalToBinary(num):
            if num >= 1:
                DecimalToBinary(num // 2)
               print num % 2 
    1bin(calculatedanswer)6def2bin(calculatedanswer)8

        

    DecimalToBinary(num):
            if num >= 1:
                DecimalToBinary(num // 2)
               print num % 2 
    1bin(calculatedanswer)6def7bin(calculatedanswer)8

        

    DecimalToBinary(num):
            if num >= 1:
                DecimalToBinary(num // 2)
               print num % 2 
    1
    DecimalToBinary(num):
            if num >= 1:
                DecimalToBinary(num // 2)
               print num % 2 
    2223
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    8
    DecimalToBinary(num):
            if num >= 1:
                DecimalToBinary(num // 2)
               print num % 2 
    5
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    1
    DecimalToBinary(num):
            if num >= 1:
                DecimalToBinary(num // 2)
               print num % 2 
    7
    One Line Code to Convert Decimal to Binary with user input

    Python3

    or 

    Python3

    if

    DecimalToBinary(num):
            if num >= 1:
                DecimalToBinary(num // 2)
               print num % 2 
    9
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    1
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    1
    1001010110001
    1010
    101011001
    2
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    3

    DecimalToBinary(num):
            if num >= 1:
                DecimalToBinary(num // 2)
               print num % 2 
    1DecimalToBinary(num):6
    The binary of the number 60 is 111100
    4
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    06
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    8
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    08

        

    1001010110001
    1010
    101011001
    5
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    1
    1001010110001
    1010
    101011001
    7

    DecimalToBinary(num):
            if num >= 1:
                DecimalToBinary(num // 2)
               print num % 2 
    1DecimalToBinary(num):6
    The binary of the number 60 is 111100
    4
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    15
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    8
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    08

    Phương pháp số 2: thập phân đến nhị phân bằng cách sử dụng hàm được xây dựng & nbsp;

    DecimalToBinary(num):
            if num >= 1:
                DecimalToBinary(num // 2)
               print num % 2 
    1DecimalToBinary(num):6
    The binary of the number 60 is 111100
    4
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    24
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    8
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    08

    Đầu ra

    1001010110001
    1010
    101011001

    Sử dụng toán tử Bitwise Shift >>.

    Python3

    def

    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    28DecimalToBinary(num):77
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    30

        

    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    32
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    1
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    34

        if

    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    37
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    1
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    1
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    40
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    30

    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    4
    The binary of the number 60 is 111100
    3
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    40

        

    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    46
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    47

    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    4
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    32
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    50
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    1
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    52
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    53__12

    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    4
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    57
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    1
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    59
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    2

        

    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    32
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    1
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    64
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    65__12

        

    The binary of the number 60 is 111100
    3
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    70

    def

    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    72

        

    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    57
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    1
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    76

        

    DecimalToBinary(num):
            if num >= 1:
                DecimalToBinary(num // 2)
               print num % 2 
    1
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    79
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    80
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    9

    if

    DecimalToBinary(num):
            if num >= 1:
                DecimalToBinary(num // 2)
               print num % 2 
    9
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    1
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    1
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    86
    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    3

        

    Input : 7                                                         
    Output :111
    
    Input :10
    Output :1010
    89

    Đầu ra

    The binary of the number 60 is 111100


    Số nhị phân 8 bit là gì?

    Số nhị phân 8 bit có thể biểu thị tối đa thập phân 255 = nhị phân 11111111. được tính như sau: 1*128+1*64+1*32+1*16+1*8+1*4+1*2+1+1+1 = thập phân 255. Đây là một số nhị phân 8 bit khác .01101011.a maximum of decimal 255= binary 11111111. Calculated as follows: 1*128 +1*64+1*32+1*16+1*8+1*4+1*2+1+1 = decimal 255. Here is another 8 bit binary number –01101011.

    Bạn có thể chuyển đổi phao thành nhị phân trong Python không?

    Python không cung cấp bất kỳ phương pháp sẵn có nào để dễ dàng chuyển đổi số thập phân điểm nổi sang số nhị phân..

    Hàm bin trong Python là gì?

    Hàm Python bin () Hàm bin () trả về phiên bản nhị phân của một số nguyên được chỉ định.Kết quả sẽ luôn bắt đầu với tiền tố 0b.returns the binary version of a specified integer. The result will always start with the prefix 0b .