python将16进制字符串转换为整数_Python 16进制与字符串的转换
電腦上裝了Python2.7和3.3兩個(gè)版本,平時(shí)運(yùn)行程序包括在Eclipse里面調(diào)試都會(huì)使用2.7,但是由于某些原因在cmd命令行中輸入python得到的解釋器則是3.3,
一直沒對(duì)此做處理,因?yàn)檫@樣可以對(duì)兩個(gè)版本的差異有一個(gè)測(cè)試,而且虛擬機(jī)里面是2.7以下的版本。
今天想到需要幾個(gè)腳本做常用的編碼轉(zhuǎn)換,這樣在沒有其他工具的情況下也可以進(jìn)行轉(zhuǎn)換,不多說上正文:
首先是2.7版本下:
2.7版本下進(jìn)行轉(zhuǎn)換還是很方便的,hex2char:output = 'data'.decode('hex')
char2hex: output = '64617461'.encode('hex')
真的是只需要用到字符串的decode和encode方法就Ok了,因此,因此如果我需要在命令行下運(yùn)行,可以這樣寫:
import sys
choose = sys.argv[1]
data = sys.argv[2]
def hex2char():
output = data.decode('hex')
print output
def char2hex():
output = data.encode('hex')
print output
print "Usage: "
if len(sys.argv) == 3:
if choose.lower() == 'hex2char':
hex2char()
if choose.lower() == 'char2hex':
char2hex()
if choose.lower()!='hex2char' and choose.lower()!='char2hex':
print "Wrong param,try again"
else:
print "Wrong number of params,check your input\n"
#this script has passed the test
這段代碼在2.7的環(huán)境下測(cè)試已經(jīng)通過,可以進(jìn)行十六進(jìn)制與字符串之間的轉(zhuǎn)換,如果覺得還不太好用,可以對(duì)代碼進(jìn)行修改修改
但是在3.0以上環(huán)境有很多用法則是不再被支持的,如果使用str.encode('hex'),則會(huì)報(bào)錯(cuò):
Traceback (most recent call last):
File "", line 1, in
'data'.encode('hex')
LookupError: unknown encoding: hex
有些人可能會(huì)說'hex'應(yīng)該為"hex",或者說遇到?jīng)]有()的情況,實(shí)際上Python中單引號(hào)和雙引號(hào)是沒什么區(qū)別的,例如:
ord('a')==97 ,ord("a")==97都是成立的
然后是3.0以上環(huán)境:
3.0環(huán)境比較常用的是binascii模塊,關(guān)于這個(gè)模塊的一些函數(shù)和方法可以查找手冊(cè),這里且說對(duì)于十六進(jìn)制和字符串的轉(zhuǎn)換
先貼代碼:
def hex2char(data):
# binascii.a2b_hex(hexstr)
output = binascii.unhexlify(data)
print(output)
def char2hex(data):
data = b'data'
# binascii.b2a_hex(data)
output = binascii.hexlify(data)
print(output)
這兩個(gè)函數(shù)與上述代碼有著相同的功能,代碼中有兩行注釋,表明binascii.a2b_hex(hexstr)和binascii.unhexlify(hexstr)在功能上是等價(jià)的,另一個(gè)同樣
這里十六進(jìn)制轉(zhuǎn)字符串直接調(diào)用就可以了,但是當(dāng)直接使用output = binascii.hexlify(data)時(shí)則報(bào)錯(cuò)了,對(duì)此函數(shù)munuals的說法是:
Return the hexadecimal representation of the binary data. Every byte of data is converted into the corresponding 2-digit hex representation. The resulting string is therefore twice as long as the length of data
因此對(duì)傳入的參數(shù)必須申明是byte of data,剛開始沒有想到,不知怎么處理,后來想到b'string data'類似于r'string data'(原始字符串,在使用windows路徑時(shí),r'..\path'可以不需要對(duì)反斜線轉(zhuǎn)義),于是有了:
data = b'data'output = binascii.hexlify(data)
于是問題便愉快的解決了,同樣可以進(jìn)行轉(zhuǎn)換
另外在2.7中,binascii模塊可以使用,output = binascii.hexlify(data)直接就可以投入使用,不必data = b'data'處理,這也是不同版本之間顯著的區(qū)別,2.7的
一些功能用起來更上手,但是3.0版這么做也是出于某種需要
再給幾個(gè)進(jìn)制轉(zhuǎn)換的例子:
int('bf',16) 將16進(jìn)制數(shù)bf轉(zhuǎn)為10進(jìn)制數(shù),把16改為8或2就對(duì)于不同的進(jìn)制
hex(num),把hex換成bin或oct就對(duì)應(yīng)于二進(jìn)制數(shù)和八進(jìn)制了
看到有一段不錯(cuò)的不錯(cuò)進(jìn)制轉(zhuǎn)換的代碼:
importos,sys
#global definition#base = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F]
base = [str(x) for x in range(10)] + [ chr(x) for x in range(ord('A'),ord('A')+6)]
#bin2dec#二進(jìn)制 to 十進(jìn)制: int(str,n=10)
defbin2dec(string_num):
return str(int(string_num, 2))
#hex2dec#十六進(jìn)制 to 十進(jìn)制
defhex2dec(string_num):
return str(int(string_num.upper(), 16))
#dec2bin#十進(jìn)制 to 二進(jìn)制: bin()
defdec2bin(string_num):
num =int(string_num)
mid =[]
whileTrue:
if num == 0: breaknum,rem = divmod(num, 2)
mid.append(base[rem])
return ''.join([str(x) for x in mid[::-1]])
最后再給出Ascii碼和整數(shù)轉(zhuǎn)換的函數(shù):
chr()函數(shù)以一個(gè)Ascii碼作為參數(shù),返回對(duì)應(yīng)的整數(shù)
ord()函數(shù)則剛好與chr()相反,返回對(duì)應(yīng)Ascii碼,如果參數(shù)超過Ascii碼表示范圍則返回對(duì)應(yīng)的unicode值
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的python将16进制字符串转换为整数_Python 16进制与字符串的转换的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux开机自动ZFS,linux –
- 下一篇: python有类似mybatis的框架_