『壹』 python 如何將文本文件中的16進制數轉換成2進制

假設文本文來件里源只有一個數字
v=eval("0x%s"%(open("txt","rb").read().strip()))
print
bin(v)
其中bin是二進制輸出
可以這樣測試一下
v=eval("0x56")
print
bin(v)

『貳』 python怎樣將16進制轉化為2進制

#coding=gbk
var=input("請輸入十六進制數:")
b=bin(int(var,16))
print(b[2:])

運行結果

詳細請參考python自帶int函數、函數用法

參考網址:https://docs.python.org/3/library/functions.html?highlight=int#bin

  1. classint(x,base=10)

Return aninteger object constructed from a number or stringx, or return0if no arguments are given. Ifxis a number, returnx.__int__(). For floating point numbers, this truncates towards zero.

Ifxis not a number or ifbaseis given, thenxmust be a string,bytes, orbytearrayinstance representing aninteger literalin radixbase. Optionally, the literal can be preceded by+or-(with no space in between) and surrounded by whitespace. A base-n literal consists of the digits 0 to n-1, withatoz(orAtoZ) having values 10 to 35. The defaultbaseis 10. The allowed values are 0 and 2–36. Base-2, -8, and -16 literals can be optionally prefixed with0b/0B,0o/0O, or0x/0X, as withinteger literals in code. Base 0 means tointerpret exactly as a code literal, so that the actual base is 2, 8, 10, or 16, and so thatint('010',0)is not legal, whileint('010')is, as well asint('010',8).

Theinteger type is described inNumeric Types — int, float, complex.

Changed in version 3.4:Ifbaseis not an instance ofintand thebaseobject has abase.__index__method, that method is called to obtain aninteger for the base. Previous versions usedbase.__int__instead ofbase.__index__.

Changed in version 3.6:Grouping digits with underscores as in code literals is allowed.

2.bin(x)

Convert aninteger number to a binary string. The result is a valid Python expression. Ifxis not a Pythonintobject, it has to define an__index__()method that returns an integer.

『叄』 Python有什麼模塊可以將文本文件轉化成二進制文件

英文就是ascii編碼嘛!
ord(num)顯示字元的編碼。
然後轉化編碼為二進制數不就可以了。
為了美化,可以每個數值添加幾個零成為8位二進制數,作為一個byte,空格隔開。
python自建有bin()函數,可是返回的字元是以'0b'開頭 # '0b000110'
可以使用下面的函數轉換

bin = lambda n : (n > 0) and (bin(n/2) + str(n%2)) or ''
bin(2) # 10

『肆』 python如何將整數轉化成二進制字元串

首先你來可以自己寫函數採用自%2的方式來算.
但是python自帶了方法 bin.
比如bin(12345)回返回字元串'0b11000000111001' 這個時候在把0b去掉即可.

bin(number).replace('0b','')

『伍』 Python如何將變數轉換為二進制的

首先你可以自己寫函數採用%2的方式來算.
但是python自帶了方法
bin.
比如bin(12345)回返回字元串'0b11000000111001'
這個時候在把0b去掉即可.
bin(number).replace('0b','')

『陸』 如何用Python將十進制數字轉為二進制,以及將二進制轉為十六進制

1、將十進制轉換成二進制,利用bin()方法。

『柒』 怎樣用python進行二進制,八進制,十進制轉換

從二進制轉換為十進制有幾種方式
第一種是在二進制數前加上0b,顯示時會自動轉換為回十進制,注意答這並不是字元串
x = 0b1010print(x)

如果是字元串可以利用eval求值

x = eval('0b1010')

第二種是利用int函數,字元串可以以0b為前綴,也可以不使用
int('1010',base=2)int('0b1010',2)

函數會將輸入base進制的字元串轉換為十進制

『捌』 在Python中,如何不用bin()把十進制轉換成二進制

十進制整數轉換為二進制整數採用"除2取余,逆序排列"法。
具體做法是:用2整除十進制整數,可以得到一個商和余數;再用2去除商,又會得到一個商和余數,如此進行,直到商為0時為止,然後把先得到的余數作為二進制數的低位有效位,後得到的余數作為二進制數的高位有效位,依次排列起來。
參考代碼
#include <stdio.h>
int main()
{
int n,a[100],i=0,j;
scanf("%d",&n);
while(n)
{
a[i++]=n%2;
n/=2;
}
for(j=i-1;j>=0;j--)
printf("%d",a[j]);
printf("\n");
return 0;
}
/*
輸出:
10
1010
*/

『玖』 python怎麼把01字元串轉為二進制bytes串

一個例子:

a="01110"

a=bytes(map(int,a))

print(a)

運行截圖:

代碼和結果的截圖

『拾』 用python的循環語句來表示十進制轉換為二進制

deftonnum(x,n=2):#轉換進制,默認是2進制
t=[]
mystr="0123456789ABCDEFGHIJK"
whilex>0:
t.append(mystr[x%n])
x//=n
t=list(reversed(t))
return''.join(t)

defmain():
x=1023
print(tonnum(x))

main()