欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

在Python中以二进制格式输入数字

发布时间:2023/12/1 51 豆豆
生活随笔 收集整理的这篇文章主要介绍了 在Python中以二进制格式输入数字 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

Syntax to convert binary value to an integer (decimal format),

将二进制值转换为整数(十进制格式)的语法,

int(bin_value, 2)

Here,

这里,

  • bin_value should contain the valid binary value

    bin_value应该包含有效的二进制值

  • 2 is the base value of the binary number system

    2是二进制数系统的基值

Note: bin_value must contain only binary digits (0 and 1), if it contains other than these digits a "ValueError" will return.

注意 : bin_value必须仅包含二进制数字(0和1),如果它不包含这些数字,则将返回“ ValueError”

将给定的二进制值转换为整数(十进制)的程序 (Program to convert given binary value to integer (decimal))

# 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))

Output

输出量

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

Now, we are going to implement the program – that will take input the number as an binary number and printing it in the decimal format.

现在,我们将实现该程序–该程序将输入数字作为二进制数字并以十进制格式打印。

程序以二进制格式输入数字 (Program to input a number in binary format)

# input number in binary format and # converting it into decimal formattry: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...")

Output

输出量

RUN 1: Input binary value: 11110000 num (decimal format): 240 num (binary format): 0b11110000RUN 2: Input binary value: 101010101010 num (decimal format): 2730 num (binary format): 0b101010101010RUN 3: Input binary value: 1111111111111111 num (decimal format): 65535 num (binary format): 0b1111111111111111RUN 4: Input binary value: 0000000 num (decimal format): 0 num (binary format): 0b0RUN 5: Input binary value: 012 Please input only binary value...

Recommended posts

推荐的帖子

  • Read input as an integer in Python

    在Python中将输入读取为整数

  • Read input as a float in Python

    在Python中以浮点形式读取输入

  • Parse a string to float in Python (float() function)

    解析要在Python中浮动的字符串(float()函数)

  • How do you read from stdin in Python?

    您如何从Python的stdin中读取信息?

  • Asking the user for integer input in Python | Limit the user to input only integer value

    要求用户在Python中输入整数| 限制用户仅输入整数值

  • Asking the user for input until a valid response in Python

    要求用户输入直到Python中的有效响应

  • Input a number in hexadecimal format in Python

    在Python中以十六进制格式输入数字

  • Input a number in octal format in Python

    在Python中以八进制格式输入数字

  • How to get the hexadecimal value of a float number in python?

    如何在python中获取浮点数的十六进制值?

  • Convert an integer value to the string using str() function in Python

    使用Python中的str()函数将整数值转换为字符串

  • Convert a float value to the string using str() function in Python

    使用Python中的str()函数将浮点值转换为字符串

  • Input and Output Operations with Examples in Python

    使用Python中的示例进行输入和输出操作

  • Taking multiple inputs from the user using split() method in Python

    使用Python中的split()方法从用户获取多个输入

  • Fast input / output for competitive programming in Python

    快速输入/输出,可在Python中进行有竞争力的编程

  • Precision handling in Python

    Python中的精确处理

  • Python print() function with end parameter

    带有结束参数的Python print()函数

翻译自: https://www.includehelp.com/python/input-a-number-in-binary-format.aspx

总结

以上是生活随笔为你收集整理的在Python中以二进制格式输入数字的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。