給個10進制轉2進制的。。你可以在根據2進制轉為8進制和16進制。4位2進制是1位16進制,3位2進制是1位8進制#include<iostream.h>int a[100];//記錄2進制數據 int Icount;//記錄2進制整數個數 int Dcount;//記錄2進制小數個數void ITen_Two(int ten)//整數轉換 { int i=0; while(ten>1) { a[i]=ten%2; ten=ten/2;//不用管奇偶 i++; Icount++; } if(ten<=1) { a[i]=1; Icount++; } }void DTen_Two(float d)//小數轉換 { int i=Icount; Dcount=0; a[i]=0; while(d!=0 && i<100) { d=d*2; if(d>=1) { a[i]=1; d=d-1; } else a[i]=0; i++; Dcount++; } }void Out() { cout<<"2進制數為:"; for(int i=Icount-1;i>=0;i--) cout<<a[i]; cout<<"."; for(int j=Icount;j<Icount+Dcount;j++) cout<<a[j]; cout<<endl; }void main() { float x; int B=1;//結束標志 while(B==1) { int I;//整數部分 float D;//小數部分 Icount=0; Dcount=0; cout<<"請輸入一個10進制浮點數:"; cin>>x; I=(int)x; D=x-I; if(I>0 && D>0) { ITen_Two(I); DTen_Two(D); } else if(I==0 && D>0) DTen_Two(D); else if(I>0 && D==0) ITen_Two(I); else cout<<"0"; if(I>0 || D>0) Out(
⑵ python中 我的int函數 我轉換8進制16進制都沒問題,轉換二進制就不行 為什麼
int類是將指定進制下的數字轉換為十進制數字
你在python命令下輸入help(int),會出現下面這段話
class int(object)
| int(x=0) -> int or long
| int(x, base=10) -> int or long
|
| Convert a number or string to an integer, or return 0 if no arguments
| are given. If x is floating point, the conversion truncates towards zero.
| If x is outside the integer range, the function returns a long instead.
|
| If x is not a number or if base is given, then x must be a string or
| Unicode object representing an integer literal in the given base. The
| literal can be preceded by '+' or '-' and be surrounded by whitespace.
| The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to
| interpret the base from the string as an integer literal.
你碰到的問題在可以引用上面這段英文來解釋
If x is not a number or if base is given, then x must be a string or
Unicode object representing an integer literal in the given base.
翻譯過來是如果參數不是數字類型,或者base參數給出了,那麼x必須是基於指定進制(默認是十進制)下的數字序列的字元串,所以下面舉得例子有的成功有的報錯:
int('123')#可以成功,執行,123十進制數字內
int('A123')#報錯,因為A不在十進制數內
int('A123',16)#可以成功,因為A123,都是十六進制內的基本數字
int('010101',2)#可以成功,因為0和1都是二進制的基本數字。
樓上的也說了,組成二進制的數字是只有0和1的,你的輸入中可是包含了非二進制的數字呢
⑶ python如何將整數轉化成二進制字元串
直接上代碼:
#coding=gbk
defintTo2Str(X,K):
"""intTo2Str(X,K)
將整數X轉化為K位2進制字元串
"""
try:
X=long(X)
except:
X=0
try:
K=int(K)
except:
K=0
ifK<1:
K=1
ifX<0:
FH=1;X=-X
else:
FH=0
A=[0forJinxrange(0,K)]
J=K-1
while(J>=0)and(X>0):
Y=X%2
X=X/2
A[J]=Y
J=J-1
ifFH==1:
#求反
forJinxrange(0,K):
ifA[J]==1:
A[J]=0
else:
A[J]=1
#末位加1
J=K-1
whileJ>=0:
A[J]=A[J]+1
ifA[J]<=1:
break;
A[J]=0
J=J-1
return"".join([chr(J+48)forJinA])
printintTo2Str(8,8)#應顯示00001000
printintTo2Str(-1,8)#應顯示10000000
⑷ 怎麼用Python做一個十進制轉二進制
題主你好,
先說下原理: 利用python內置的函數bin()即可.
代碼截圖:
=====
希望可以幫到題主, 歡迎追問.
⑸ 怎樣用python進行二進制,八進制,十進制轉換
從二進制轉換為十進制有幾種方式
第一種是在二進制數前加上0b,顯示時會自動轉換為十進制,注意這並不是字元串
x = 0b1010print(x)
如果是字元串可以利用eval求值
x = eval('0b1010')
第二種是利用int函數,字元串可以以0b為前綴,也可以不使用
int('1010',base=2)int('0b1010',2)
函數會將輸入base進制的字元串轉換為十進制
⑹ python中怎樣將十進制數轉化為二進制
#!/usr/bin/python# -*- coding:utf-8 -*-# @Time : 2018/6/19 10:20# @Author : # @File : Dec_To_Bin.py"""十進制轉二進制""" # 定義一個十進制轉二進制的函數def dec2bin(string_num): num = int(string_num) # 將傳入的字元串數字轉換成整型 mid = [] # 定義一個空列表 while True: # 循環,條件為真時執行 if num == 0: # 當輸入值是0時,直接跳出循環 break num, rem = divmod(num, 2) # 調用函數divmod,得到商num,和余數rem mid.append(rem) # 將余數存入列表 return ''.join([str(x) for x in mid[::-1]]) # 返回結果,列表取反後拼接成字元串 if __name__ == '__main__': anum = raw_input(u'請輸入要轉換的數字:') print u'該數字轉換為二進制後是:{}'.format(dec2bin(anum))
⑺ python十進制轉二進制代碼(不用內置函數,使用if,else for,in,while)
n = int(input('請輸入要轉換進制的數值:'))
b = []
while True: # 一直循環,商為0時利用break退出循環
s = n // 2 # 商
y = n % 2 # 余數
b = b + [y] # 每一個余數存儲到b中
print b
if s == 0:
break # 余數為0時結束循環
n = s
b.reverse() # 使b中的元素反向排列
b = [ str(i)for i in b ]
b = ['0b'] + b
print ('該數字轉換為二進制後是:')
print ''.join(b)
⑻ 編寫一個python程序轉換十進制整數到二進制整數的轉換
bin函數可以直接轉換。不過開頭有0b兩個字元,可以用切片顯示後面部分。
print(bin(int(input()))[2:])
如果非要用純代碼轉換就用循環。
n=int(input())
ans=''
while n%2!=0:
縮進s+=n%2
縮進n//=2
print(ans[::-1])
⑼ Python語言編程實現由十進制數到二進制數的轉換
#!/usr/bin/python
#-*-coding:utf-8-*-
#@Time:2018/6/1814:04
#@File:Dec_To_Bin.py
"""
description
"""
defdec2bin(string_num):
num=int(string_num)
mid=[]
whileTrue:
ifnum==0:
break
num,rem=divmod(num,2)
mid.append(rem)
return''.join([str(x)forxinmid[::-1]])
if__name__=='__main__':
anum=raw_input(u'請輸入要轉換的數字:')
printu'該數字轉換為二進制後是:{}'.format(dec2bin(anum))