在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 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...")

Output

輸出量

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...

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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/378026.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/378026.shtml
英文地址,請注明出處:http://en.pswp.cn/news/378026.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

八、邊緣保留濾波(EPF)

一、概念 邊緣保留濾波(EPF,edge preserving filtering) 二、高斯雙邊 cv2.bilateralFilter(image,0,100,15)100為差異,15為周圍的區域 import cv2 import numpy as npdef bilateralFilter(image):dst cv2.bilateralFilter(image,0,100,15)cv2.imshow(bilater…

LintCode 375. 克隆二叉樹(深復制)

先序遍歷構造二叉樹 TreeNode * preorder(TreeNode * root){if(rootNULL) return NULL;TreeNode * ans;ansnew TreeNode(root->val);if(root->left!NULL){ans->leftpreorder(root->left);}if(root->right!NULL){ans->rightpreorder(root->right);}return…

關于ECMAScript基礎入門的分享

目錄 ECMAScript基礎入門1. 介紹2. 變量與數據類型2.1 變量2.2 數據類型 3. 運算符3.1 算術運算符3.2 比較運算符 4. 控制流4.1 條件語句4.2 循環語句 5. 函數6. 對象與數組6.1 對象6.2 數組 7. 總結 ECMAScript基礎入門 1. 介紹 ECMAScript是JavaScript的標準規范&#xff0…

kotlin 計算平方_Kotlin程序來計算復利

kotlin 計算平方Compound interest is the sum of principal amount and interest of interest. 復利是本金和利息之和。 Given, principal, rate, and time, we have to calculate the Compound interest. 給定本金,利率和時間,我們必須計算復利。 Fo…

近代科學為什么誕生在西方-1

寬泛的講,近代科學是幾種文明在長達幾個世紀的持續交流碰撞中產生的。它正在日益成為全世界全人類都有效的普適科學。通向現代科學之路就是通向自由和開放交流之路。 馬克思韋伯和莫頓都認為,科學事業要持續的進步就要特定的文化和制度的支持。 中國的數…

九、圖像直方圖

一、圖像直方圖的屬性 說白了就是將圖像上的各個顏色通道上的像素點的像素值進行統計,例如:像素值為14的像素點個數有幾個,進行顯示。 圖像的像素值取值范圍為[0,255],這個范圍也成為直方圖的range也就是直方圖的橫坐標軸 每一個…

BIFR的完整形式是什么?

BIFR:工業和金融重組委員會 (BIFR: Board of Industrial and Financial Reconstruction) BIFR is an abbreviation of the Board of Industrial and Financial Reconstruction. It was an organization of the Government of India and a branch of the Department …

LeetCode 101. 對稱二叉樹 思考分析

題目 給定一個二叉樹,檢查它是否是鏡像對稱的。 例如,二叉樹 [1,2,2,3,4,4,3] 是對稱的。 1/ 2 2 / \ / 3 4 4 3 但是下面這個 [1,2,2,null,3,null,3] 則不是鏡像對稱的: 1/ 2 2 \ 3 3 進階: 你可以運用遞歸和迭代兩種方法解決這個…

內心能不能寧靜一點,做事能不能堅持一下

內心能不能寧靜一點,做事能不能堅持一下 每次朋友問我怎么樣,我總感覺不好回答,如果說實話我想他們或許是不能理解我的處境的,只能報以“還好”之類的語言,糊弄一下。唯一一次說了實話是:我墜落了&#xff…

直方圖反向投影

通過直方圖反向投影,根據目標衣服顏色的特征來進行定位 cv2.calcHist([roi_hsv],[0,1],None,[32,48],[0,180,0,256])其中[32,48]表示bin的個數,可以修改,當然范圍越小越精確 import cv2 import numpy as np from matplotlib import pyplot …

javascript 排序_JavaScript中的排序方法

javascript 排序There are tons of sorting algorithms available like bubble sort, merge sort, insertion sort etc. You must have implemented some of these in other programming languages like C or C. But in this article, I will be demonstrating the Sorting met…

LeetCode 二叉樹、N叉樹的最大深度與最小深度(遞歸解)

目錄104. 二叉樹的最大深度559. N叉樹的最大深度111. 二叉樹的最小深度之前的筆記中,已經用層序遍歷解決過這個問題了現在試著用深度的解法去求解104. 二叉樹的最大深度 給定一個二叉樹,找出其最大深度。 二叉樹的深度為根節點到最遠葉子節點的最長路徑…

十、模板匹配

一、概念 模板匹配就是在整個圖像區域發現與給定子圖像匹配的小塊區域。 需要首先給定一個模板圖像A,和一個待檢測圖像B。 在待檢測圖像B上,從左往右,從上往下計算待檢測圖像B和模板圖像A所重疊的匹配度,匹配度越高則兩者相同的可…

基于WF的意見征集4(淺析)

接口項目&#xff1a;IClass&#xff08;項目名稱&#xff09; HTHuiFuusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Workflow.Runtime;using System.Workflow.Activities;namespace IClass{ /// <summary> /…

那些VisualStudio隱藏的調試功能

VisualStudio是一個強大的調試工具&#xff0c;里面很多隱藏功能少有人問津&#xff0c;但是在特定場景可以節省你很多時間&#xff0c;本文主要介紹一些VisualStudio調試相關的隱藏功能&#xff0c;歡迎大家補充。 運行到指針(Run to cursor) 大多數人用Visual Studio在調試程…

php連接數據庫代碼_PHP代碼連接各種數據庫

php連接數據庫代碼1)用PHP連接MySQL (1) Connecting with MySQL in PHP) <?php$host "localhost";$uname "username";$pw "password";$db "newDB";try {$conn new PDO("mysql:host$host;dbname$db", $uname, $pw);…

【C++ grammar】對象和類(創建對象、對象拷貝、分離聲明與實現)

目錄1、用類創建對象1、面向對象的特征2、對象由什么構成3、如何定義對象4、創建對象并訪問對象成員1. Constructors(構造函數)2. Constructing Objects (創建對象)3. Object Member Access Operator(對象訪問運算符)2、對象拷貝以及分離聲明與實現1、類是一種數據類型1.1. 定義…

十一、圖像二值化

一、二值圖像 其實就是把圖像轉換為只有黑白的兩種顏色圖像&#xff0c;即像素值非零即一 三角閾值二值化 對一個圖像進行操作&#xff0c;獲取圖像的直方圖&#xff0c;找到波峰和波谷進行連線設為線段A&#xff0c;每個點做有關線段A的垂線垂足在線段A上&#xff0c;最后將…

百度地圖LV1.5實踐項目開發工具類bmap.util.jsV1.2

/*** 百度地圖使用工具類-v1.5* * author boonya* date 2013-7-7* address Chengdu,Sichuan,China* email boonyasina.com* company KWT.Shenzhen.Inc.com* notice 有些功能需要加入外部JS庫才能使用&#xff0c;另外還需要申請地圖JS key .* 申請地址&#xff1a;http…

isatty_帶有示例的Python File isatty()方法

isatty文件isatty()方法 (File isatty() Method) isatty() method is an inbuilt method in Python, it is used to check whether a file stream is an interactive or not in Python i.e. a file stream is connected to a terminal device. If a file is connected to a ter…