Hướng dẫn how to take binary string as input in python - cách lấy chuỗi nhị phân làm đầu vào 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.

N,M=raw_input[].split[' ']
N=int[N,2]
M=int[M,2]
print N|M

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

Hướng dẫn Python

111 101

Mã chuyển đổi đầu vào ban đầu thành nhị phân và sau đó áp dụng bitwise hoặc. Tuy nhiên, những gì tôi yêu cầu là một cách để nhận các đầu vào ban đầu trong chính nhị phân.

Nếu đầu vào làApr 10, 2015 at 18:27

0

Đầu ra được đưa ra là 7. Đầu ra tôi muốn là ____10 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]]
1.

print format[N|M, 'b']

Hỏi ngày 10 tháng 4 năm 2015 lúc 18:27

>>> format[7, 'b']
'111'

Chỉ định dạng đầu ra trở lại nhị phân:

binary1, binary2 = raw_input[].split[' ']
N = int[binary1, 2]
M = int[binary2, 2]
print '{}|{} makes {:b}'.format[binary1, binary2, N|M]

Điều này sử dụng hàm

# 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 để tạo lại chuỗi nhị phân; Định dạng
# 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 tạo ra đầu ra nhị phân cơ sở-2:

Đã trả lời ngày 10 tháng 4 năm 2015 lúc 18:28Apr 10, 2015 at 18:28

Martijn Pieters ♦ Martijn PietersMartijn Pieters

996K277 Huy hiệu vàng3920 Huy hiệu bạc3262 Huy hiệu Đồng277 gold badges3920 silver badges3262 bronze badges

Bài Viết Liên Quan

Chủ Đề