電腦上裝了Python2.7和3.3兩個版本,平時運行程序包括在Eclipse里面調試都會使用2.7,但是由於某些原因在cmd命令行中輸入python得到的解釋器則是3.3,
一直沒對此做處理,因為這樣可以對兩個版本的差異有一個測試,而且虛擬機里面是2.7以下的版本。
今天想到需要幾個腳本做常用的編碼轉換,這樣在沒有其他工具的情況下也可以進行轉換,不多說上正文:
首先是2.7版本下:
2.7版本下進行轉換還是很方便的,hex2char:output = 'data'.decode('hex')
char2hex: output = '64617461'.encode('hex')
真的是只需要用到字符串的decode和encode方法就Ok了,因此,因此如果我需要在命令行下運行,可以這樣寫:
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: <filename> <hex2char or char2hex> <your data>"
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的環境下測試已經通過,可以進行十六進制與字符串之間的轉換,如果覺得還不太好用,可以對代碼進行修改修改
但是在3.0以上環境有很多用法則是不再被支持的,如果使用str.encode('hex'),則會報錯:
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
'data'.encode('hex')
LookupError: unknown encoding: hex
有些人可能會說'hex'應該為"hex",或者說遇到沒有()的情況,實際上Python中單引號和雙引號是沒什么區別的,例如:
ord('a')==97 ,ord("a")==97都是成立的
然后是3.0以上環境:
3.0環境比較常用的是binascii模塊,關於這個模塊的一些函數和方法可以查找手冊,這里且說對於十六進制和字符串的轉換
先貼代碼:
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)
這兩個函數與上述代碼有着相同的功能,代碼中有兩行注釋,表明binascii.a2b_hex(hexstr)和binascii.unhexlify(hexstr)在功能上是等價的,另一個同樣
這里十六進制轉字符串直接調用就可以了,但是當直接使用output = binascii.hexlify(data)時則報錯了,對此函數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
因此對傳入的參數必須申明是byte of data,剛開始沒有想到,不知怎么處理,后來想到b'string data'類似於r'string data'(原始字符串,在使用windows路徑時,r'..\path'可以不需要對反斜線轉義),於是有了:
data = b'data'output = binascii.hexlify(data)
於是問題便愉快的解決了,同樣可以進行轉換
另外在2.7中,binascii模塊可以使用,output = binascii.hexlify(data)直接就可以投入使用,不必data = b'data'處理,這也是不同版本之間顯著的區別,2.7的
一些功能用起來更上手,但是3.0版這么做也是出於某種需要
再給幾個進制轉換的例子:
int('bf',16) 將16進制數bf轉為10進制數,把16改為8或2就對於不同的進制
hex(num),把hex換成bin或oct就對應於二進制數和八進制了
看到有一段不錯的不錯進制轉換的代碼:
import os,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# 二進制 to 十進制: int(str,n=10) def bin2dec(string_num): return str(int(string_num, 2))# hex2dec# 十六進制 to 十進制def hex2dec(string_num): return str(int(string_num.upper(), 16))# dec2bin# 十進制 to 二進制: bin() def dec2bin(string_num): num = int(string_num) mid = [] while True: if num == 0: break num,rem = divmod(num, 2) mid.append(base[rem]) return ''.join([str(x) for x in mid[::-1]])
完整代碼見http://www.cnblogs.com/zhangpengshou/archive/2012/03/12/2392068.html
最后再給出Ascii碼和整數轉換的函數:
chr()函數以一個Ascii碼作為參數,返回對應的整數
ord()函數則剛好與chr()相反,返回對應Ascii碼,如果參數超過Ascii碼表示范圍則返回對應的unicode值
---------------------
作者:r00tgrok
來源:CNBLOGS
原文:https://www.cnblogs.com/r00tgrok/p/hex2char.html
版權聲明:本文為作者原創文章,轉載請附上博文鏈接!