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

电脑上装了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: "

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 "", line 1, in

'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就对应于二进制数和八进制了

看到有一段不错的不错进制转换的代码:

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#二进制 to 十进制: int(str,n=10)defbin2dec(string_num):return str(int(string_num, 2))#hex2dec#十六进制 to 十进制defhex2dec(string_num):return str(int(string_num.upper(), 16))#dec2bin#十进制 to 二进制: 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码和整数转换的函数:

chr()函数以一个Ascii码作为参数,返回对应的整数

ord()函数则刚好与chr()相反,返回对应Ascii码,如果参数超过Ascii码表示范围则返回对应的unicode值

---------------------

作者:r00tgrok

来源:CNBLOGS

原文:https://www.cnblogs.com/r00tgrok/p/hex2char.html

版权声明:本文为作者原创文章,转载请附上博文链接!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值