日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

[转]Python十六进制与字符串的转换

發(fā)布時間:2024/6/21 综合教程 34 生活家
生活随笔 收集整理的這篇文章主要介紹了 [转]Python十六进制与字符串的转换 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

  電腦上裝了Python2.7和3.3兩個版本,平時運(yùn)行程序包括在Eclipse里面調(diào)試都會使用2.7,但是由于某些原因在cmd命令行中輸入python得到的解釋器則是3.3,

  一直沒對此做處理,因為這樣可以對兩個版本的差異有一個測試,而且虛擬機(jī)里面是2.7以下的版本。

  今天想到需要幾個腳本做常用的編碼轉(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: <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 "

#this script has passed the test


這段代碼在2.7的環(huán)境下測試已經(jīng)通過,可以進(jìn)行十六進(jìn)制與字符串之間的轉(zhuǎn)換,如果覺得還不太好用,可以對代碼進(jìn)行修改修改

但是在3.0以上環(huán)境有很多用法則是不再被支持的,如果使用str.encode('hex'),則會報錯:

Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
'data'.encode('hex')
LookupError: unknown encoding: hex

有些人可能會說'hex'應(yīng)該為"hex",或者說遇到?jīng)]有()的情況,實(shí)際上Python中單引號和雙引號是沒什么區(qū)別的,例如:

ord('a')==97 ,ord("a")==97都是成立的

然后是3.0以上環(huán)境:

3.0環(huán)境比較常用的是binascii模塊,關(guān)于這個模塊的一些函數(shù)和方法可以查找手冊,這里且說對于十六進(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)

這兩個函數(shù)與上述代碼有著相同的功能,代碼中有兩行注釋,表明binascii.a2b_hex(hexstr)和binascii.unhexlify(hexstr)在功能上是等價的,另一個同樣

這里十六進(jìn)制轉(zhuǎn)字符串直接調(diào)用就可以了,但是當(dāng)直接使用output = binascii.hexlify(data)時則報錯了,對此函數(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

因此對傳入的參數(shù)必須申明是byte of data,剛開始沒有想到,不知怎么處理,后來想到b'string data'類似于r'string data'(原始字符串,在使用windows路徑時,r'..path'可以不需要對反斜線轉(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版這么做也是出于某種需要

再給幾個進(jìn)制轉(zhuǎn)換的例子:

int('bf',16) 將16進(jìn)制數(shù)bf轉(zhuǎn)為10進(jìn)制數(shù),把16改為8或2就對于不同的進(jìn)制
hex(num),把hex換成bin或oct就對應(yīng)于二進(jìn)制數(shù)和八進(jìn)制了

看到有一段不錯的不錯進(jìn)制轉(zhuǎn)換的代碼:

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# 二進(jìn)制 to 十進(jìn)制: int(str,n=10) def bin2dec(string_num):    return str(int(string_num, 2))# hex2dec# 十六進(jìn)制 to 十進(jìn)制def hex2dec(string_num):    return str(int(string_num.upper(), 16))# dec2bin# 十進(jìn)制 to 二進(jìn)制: 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碼和整數(shù)轉(zhuǎn)換的函數(shù):

chr()函數(shù)以一個Ascii碼作為參數(shù),返回對應(yīng)的整數(shù)

ord()函數(shù)則剛好與chr()相反,返回對應(yīng)Ascii碼,如果參數(shù)超過Ascii碼表示范圍則返回對應(yīng)的unicode值

    

  

---------------------
作者:r00tgrok
來源:CNBLOGS
原文:https://www.cnblogs.com/r00tgrok/p/hex2char.html
版權(quán)聲明:本文為作者原創(chuàng)文章,轉(zhuǎn)載請附上博文鏈接!

總結(jié)

以上是生活随笔為你收集整理的[转]Python十六进制与字符串的转换的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。