Hướng dẫn how do you enter binary in python? - làm thế nào để bạn nhập nhị phân trong python?

Trang chủ »Python

Python | Đầu vào nhị phân: Ở đây, chúng ta sẽ học cách nhập một số ở định dạng nhị phân trong ngôn ngữ lập trình Python? Được gửi bởi bao gồm bao gồm, vào ngày 27 tháng 4 năm 2020: Here, we are going to learn how to Input a number in binary format in Python programming language?
Submitted by IncludeHelp, on April 27, 2020

Cú pháp để chuyển đổi giá trị nhị phân thành số nguyên (định dạng thập phân),

    int(bin_value, 2)

Here,

  • bin_value phải chứa giá trị nhị phân hợp lệ
  • 2 là giá trị cơ sở của hệ thống nhị phân

Lưu ý: bin_value chỉ phải chứa các chữ số nhị phân (0 và 1), nếu nó chứa các chữ số này, một chữ số "giá trị" sẽ trở lại.: bin_value must contain only binary digits (0 and 1), if it contains other than these digits a "ValueError" will return.

Chương trình chuyển đổi giá trị nhị phân đã cho thành số nguyên (thập phân)

# function to convert given binary Value
# to an integer (decimal number)
def BinToDec(value):
  try:
    return int(value, 2)
  except ValueError:
    return "Invalid binary Value"

# Main code
input1 = "11110000"
input2 = "10101010"
input3 = "11111111"
input4 = "000000"
input5 = "012"

print(input1, "as decimal: ", BinToDec(input1))
print(input2, "as decimal: ", BinToDec(input2))
print(input3, "as decimal: ", BinToDec(input3))
print(input4, "as decimal: ", BinToDec(input4))
print(input5, "as decimal: ", BinToDec(input5))

Đầu ra

11110000 as decimal:  240
10101010 as decimal:  170
11111111 as decimal:  255
000000 as decimal:  0
012 as decimal:  Invalid binary Value

Bây giờ, chúng tôi sẽ thực hiện chương trình - sẽ lấy số đầu vào làm số nhị phân và in nó theo định dạng thập phân.

Chương trình nhập một số ở định dạng nhị phân

# input number in binary format and 
# converting it into decimal format

try:
  num = int(input("Input binary value: "), 2)
  print("num (decimal format):", num)
  print("num (binary format):", bin(num))  
except ValueError:
  print("Please input only binary value...")

Đầu ra

RUN 1:
Input binary value: 11110000
num (decimal format): 240
num (binary format): 0b11110000

RUN 2:
Input binary value: 101010101010
num (decimal format): 2730
num (binary format): 0b101010101010

RUN 3:
Input binary value: 1111111111111111
num (decimal format): 65535
num (binary format): 0b1111111111111111

RUN 4:
Input binary value: 0000000
num (decimal format): 0
num (binary format): 0b0

RUN 5:
Input binary value: 012
Please input only binary value...

Bây giờ, chúng tôi sẽ thực hiện chương trình - sẽ lấy số đầu vào làm số nhị phân và in nó theo định dạng thập phân.


Làm thế nào để bạn thể hiện văn học nhị phân trong Python?

Chúng không phải là "nhị phân" theo nghĩa đen, mà là, "chữ số nguyên". Bạn có thể thể hiện các chữ số nguyên với định dạng nhị phân với

# function to convert given binary Value
# to an integer (decimal number)
def BinToDec(value):
  try:
    return int(value, 2)
  except ValueError:
    return "Invalid binary Value"

# Main code
input1 = "11110000"
input2 = "10101010"
input3 = "11111111"
input4 = "000000"
input5 = "012"

print(input1, "as decimal: ", BinToDec(input1))
print(input2, "as decimal: ", BinToDec(input2))
print(input3, "as decimal: ", BinToDec(input3))
print(input4, "as decimal: ", BinToDec(input4))
print(input5, "as decimal: ", BinToDec(input5))
4, sau đó là
# function to convert given binary Value
# to an integer (decimal number)
def BinToDec(value):
  try:
    return int(value, 2)
  except ValueError:
    return "Invalid binary Value"

# Main code
input1 = "11110000"
input2 = "10101010"
input3 = "11111111"
input4 = "000000"
input5 = "012"

print(input1, "as decimal: ", BinToDec(input1))
print(input2, "as decimal: ", BinToDec(input2))
print(input3, "as decimal: ", BinToDec(input3))
print(input4, "as decimal: ", BinToDec(input4))
print(input5, "as decimal: ", BinToDec(input5))
5 hoặc
# function to convert given binary Value
# to an integer (decimal number)
def BinToDec(value):
  try:
    return int(value, 2)
  except ValueError:
    return "Invalid binary Value"

# Main code
input1 = "11110000"
input2 = "10101010"
input3 = "11111111"
input4 = "000000"
input5 = "012"

print(input1, "as decimal: ", BinToDec(input1))
print(input2, "as decimal: ", BinToDec(input2))
print(input3, "as decimal: ", BinToDec(input3))
print(input4, "as decimal: ", BinToDec(input4))
print(input5, "as decimal: ", BinToDec(input5))
6, sau đó là một loạt các số không và các số không, ví dụ::

>>> 0b0010101010
170
>>> 0B010101
21

Từ Python 3 Docs, đây là những cách cung cấp các chữ số nguyên trong Python:

Các chữ số nguyên được mô tả bằng các định nghĩa từ vựng sau:

integer      ::=  decinteger | bininteger | octinteger | hexinteger
decinteger   ::=  nonzerodigit (["_"] digit)* | "0"+ (["_"] "0")*
bininteger   ::=  "0" ("b" | "B") (["_"] bindigit)+
octinteger   ::=  "0" ("o" | "O") (["_"] octdigit)+
hexinteger   ::=  "0" ("x" | "X") (["_"] hexdigit)+
nonzerodigit ::=  "1"..."9"
digit        ::=  "0"..."9"
bindigit     ::=  "0" | "1"
octdigit     ::=  "0"..."7"
hexdigit     ::=  digit | "a"..."f" | "A"..."F"

Không có giới hạn cho độ dài của các chữ số nguyên ngoài những gì có thể được lưu trữ trong bộ nhớ có sẵn.

Lưu ý rằng các số không hàng đầu trong một số thập phân khác không được phép. Điều này là để định hướng với các chữ theo nghĩa ba kiểu chữ C, mà Python đã sử dụng trước phiên bản 3.0.

Một số ví dụ về chữ số nguyên:

7     2147483647                        0o177    0b100110111
3     79228162514264337593543950336     0o377    0xdeadbeef
      100_000_000_000                   0b_1110_0101

Đã thay đổi trong phiên bản 3.6: Dấu gạch dưới hiện được phép nhóm các mục đích nhóm trong nghĩa đen.

Những cách khác để thể hiện nhị phân:

Bạn có thể có các số không và các số không trong một đối tượng chuỗi có thể được thao tác (mặc dù có lẽ bạn chỉ nên thực hiện các thao tác bitwise trên số nguyên trong hầu hết các trường hợp) - chỉ cần chuyển int chuỗi số 0 và cơ sở bạn đang chuyển đổi từ (2 ):

>>> int('010101', 2)
21

Bạn có thể tùy chọn có tiền tố

# function to convert given binary Value
# to an integer (decimal number)
def BinToDec(value):
  try:
    return int(value, 2)
  except ValueError:
    return "Invalid binary Value"

# Main code
input1 = "11110000"
input2 = "10101010"
input3 = "11111111"
input4 = "000000"
input5 = "012"

print(input1, "as decimal: ", BinToDec(input1))
print(input2, "as decimal: ", BinToDec(input2))
print(input3, "as decimal: ", BinToDec(input3))
print(input4, "as decimal: ", BinToDec(input4))
print(input5, "as decimal: ", BinToDec(input5))
7 hoặc
# function to convert given binary Value
# to an integer (decimal number)
def BinToDec(value):
  try:
    return int(value, 2)
  except ValueError:
    return "Invalid binary Value"

# Main code
input1 = "11110000"
input2 = "10101010"
input3 = "11111111"
input4 = "000000"
input5 = "012"

print(input1, "as decimal: ", BinToDec(input1))
print(input2, "as decimal: ", BinToDec(input2))
print(input3, "as decimal: ", BinToDec(input3))
print(input4, "as decimal: ", BinToDec(input4))
print(input5, "as decimal: ", BinToDec(input5))
8:

>>> int('0b0010101010', 2)
170

Nếu bạn vượt qua nó

# function to convert given binary Value
# to an integer (decimal number)
def BinToDec(value):
  try:
    return int(value, 2)
  except ValueError:
    return "Invalid binary Value"

# Main code
input1 = "11110000"
input2 = "10101010"
input3 = "11111111"
input4 = "000000"
input5 = "012"

print(input1, "as decimal: ", BinToDec(input1))
print(input2, "as decimal: ", BinToDec(input2))
print(input3, "as decimal: ", BinToDec(input3))
print(input4, "as decimal: ", BinToDec(input4))
print(input5, "as decimal: ", BinToDec(input5))
4 làm cơ sở, nó sẽ giả sử cơ sở 10 nếu chuỗi không chỉ định với tiền tố:

# function to convert given binary Value
# to an integer (decimal number)
def BinToDec(value):
  try:
    return int(value, 2)
  except ValueError:
    return "Invalid binary Value"

# Main code
input1 = "11110000"
input2 = "10101010"
input3 = "11111111"
input4 = "000000"
input5 = "012"

print(input1, "as decimal: ", BinToDec(input1))
print(input2, "as decimal: ", BinToDec(input2))
print(input3, "as decimal: ", BinToDec(input3))
print(input4, "as decimal: ", BinToDec(input4))
print(input5, "as decimal: ", BinToDec(input5))
0

Chuyển đổi từ int trở lại nhị phân có thể đọc được của con người:

Bạn có thể chuyển một số nguyên cho thùng để xem biểu diễn chuỗi của một chữ nhị phân:

# function to convert given binary Value
# to an integer (decimal number)
def BinToDec(value):
  try:
    return int(value, 2)
  except ValueError:
    return "Invalid binary Value"

# Main code
input1 = "11110000"
input2 = "10101010"
input3 = "11111111"
input4 = "000000"
input5 = "012"

print(input1, "as decimal: ", BinToDec(input1))
print(input2, "as decimal: ", BinToDec(input2))
print(input3, "as decimal: ", BinToDec(input3))
print(input4, "as decimal: ", BinToDec(input4))
print(input5, "as decimal: ", BinToDec(input5))
1

Và bạn có thể kết hợp

11110000 as decimal:  240
10101010 as decimal:  170
11111111 as decimal:  255
000000 as decimal:  0
012 as decimal:  Invalid binary Value
0 và
11110000 as decimal:  240
10101010 as decimal:  170
11111111 as decimal:  255
000000 as decimal:  0
012 as decimal:  Invalid binary Value
1 để qua lại:

# function to convert given binary Value
# to an integer (decimal number)
def BinToDec(value):
  try:
    return int(value, 2)
  except ValueError:
    return "Invalid binary Value"

# Main code
input1 = "11110000"
input2 = "10101010"
input3 = "11111111"
input4 = "000000"
input5 = "012"

print(input1, "as decimal: ", BinToDec(input1))
print(input2, "as decimal: ", BinToDec(input2))
print(input3, "as decimal: ", BinToDec(input3))
print(input4, "as decimal: ", BinToDec(input4))
print(input5, "as decimal: ", BinToDec(input5))
2

Bạn cũng có thể sử dụng một đặc tả định dạng, nếu bạn muốn có chiều rộng tối thiểu với số không trước:

# function to convert given binary Value
# to an integer (decimal number)
def BinToDec(value):
  try:
    return int(value, 2)
  except ValueError:
    return "Invalid binary Value"

# Main code
input1 = "11110000"
input2 = "10101010"
input3 = "11111111"
input4 = "000000"
input5 = "012"

print(input1, "as decimal: ", BinToDec(input1))
print(input2, "as decimal: ", BinToDec(input2))
print(input3, "as decimal: ", BinToDec(input3))
print(input4, "as decimal: ", BinToDec(input4))
print(input5, "as decimal: ", BinToDec(input5))
3

Làm thế nào để bạn tuyên bố nhị phân?

Để chỉ định một chữ nhị phân, hãy thêm tiền tố 0b hoặc 0b vào giá trị tích phân. Vì vậy, chúng ta có thể thể hiện các chữ số nhị phân trong một chương trình bằng cách gán chúng cho các biến và đầu ra của các biến đó sau khi thực thi chương trình sẽ là các chữ số thập phân.add the prefix 0b or 0B to the integral value. So, we can express binary digits in a program by assigning them to variables, and the output of those variables after executing the program will be decimal digits.

Làm thế nào để bạn nhập số nhị phân?

Chương trình nhập một số ở định dạng nhị phân # Số đầu vào ở định dạng nhị phân và # Chuyển đổi nó thành định dạng thập phân Hãy thử: num = int (đầu vào ("Giá trị nhị phân đầu vào:"), 2) in ("num (định dạng thập phân):",,,num) in ("num (định dạng nhị phân):", bin (num)) ngoại trừ valueError: in ("vui lòng chỉ nhập giá trị nhị phân ...")num = int(input("Input binary value: "), 2) print("num (decimal format):", num) print("num (binary format):", bin(num)) except ValueError: print("Please input only binary value...")

Có một chức năng nhị phân trong Python?

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.The bin() function returns the binary version of a specified integer. The result will always start with the prefix 0b .

Loại nhị phân trong Python là gì?

Các byte và bytearrays được sử dụng để thao tác dữ liệu nhị phân trong Python.Các byte và bytebry này được hỗ trợ bởi giao thức bộ đệm, được đặt tên là MemoryView.MemoryView có thể truy cập bộ nhớ của đối tượng nhị phân khác mà không cần sao chép dữ liệu thực tế.Các chữ byte có thể được hình thành bởi các tùy chọn này.byte and bytearrays are used to manipulate binary data in python. These bytes and bytearrys are supported by buffer protocol, named memoryview. The memoryview can access the memory of other binary object without copying the actual data. The byte literals can be formed by these options.