
正文
关于十六进制函数python的信息
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
关于python如何实现各进制转换的总结大全
ctf经常遇到进制转换的问题,就正好做一个进制转换总结,分享出来供大家参考学习,下面来一起看看详细的介绍:
字符串与十六进制转换
例如百度ctf 12月的第二场第一个misc
?
1
666C61677B65633862326565302D336165392D346332312D613031322D3038616135666137626536377D
比较简单的一种做法就是直接调用字符串的.decode('hex')解密即可, 但如果不用这个函数你会怎么解呢?
一种思路就是先2个分组,解出每组的ascii值,合并下字符串即可得到,具体代码如下
?
1234567
import res='666C61677B65633862326565302D336165392D346332312D613031322D3038616135666137626536377D's = re.findall(r'.{2}',s)s = map(lambda x:chr(int(x,16)),s)print ''.join(s)flag{ec8b2ee0-3ae9-4c21-a012-08aa5fa7be67}
前面说了字符串的decode('hex')函数,另外还有两个转16进制的函数,这里都总结一下
内置函数hex()
只能转换10进制整数为十六进制,不能转字符串
binascii库的hexlify()和b2a_hex()
这两个函数的功能是将字符串转换成十六进制,对应的解密函数分别为 unhexlify()和a2b_hex()
进制互转
二进制,八进制,十六进制转10进制比较简单,直接调用
int函数
?
1
int(str,base) //返回十进制整数,但注意此时第一个参数为字符串
对应的解密函数分别是
?
12345
bin() //10进制转二进制 oct() //十进制转八进制 hex() //十进制转十六进制
但二进制直接转16进制就需要多走一步了,先用int转十进制,在用上面提到的hex()函数将十进制转换成十六进制,比较精简的写法是
?
1
map(lambda x:hex(int(x,2)),['0011']) //lambda表达式
或者是
?
1
[hex(int(x,2)) for x in ['0011']] //列表解析
对应的解密函数就是
?
1
map(lambda x:bin(int(x,16)),['ef'])
最后在附上自己用python写的一个进制转换小工具,主要功能是对一组二进制,或者ascii,或十六进制转换成字符串,想必ctf上也经常会遇到这类题型吧
?
1234567891011121314151617181920212223242526272829303132333435363738394041424344
# make by 江sir#coding:utf-8import reimport argparse def bintostr(text): text = text.replace(' ','') text = re.findall(r'.{8}',text) s = map(lambda x:chr(int(x,2)),text) #批量二进制转十进制 flag = ''.join(s) return flag def asciitostr(text): if ' ' in text: text = text.split(' ') elif ',' in text: text = text.split(',') s = map(lambda x:chr(int(x)),text) flag = ''.join(s) return flag def hextostr(text): text = re.findall(r'.{2}',text) #print text s = map(lambda x:chr(int(x,16)),text) #print s flag = ''.join(s) return flag if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument("-b") parser.add_argument("-a") parser.add_argument("-x") argv = parser.parse_args() #print argv if argv.b: res = bintostr(argv.b) elif argv.a: res = asciitostr(argv.a) elif argv.x: res = hextostr(argv.x) print res
用法:
十六进制转字符串:
666C61677B65633862326565302D336165392D346332312D613031322D3038616135666137626536377D
?
12
bintostr.py -x "666C61677B65633862326565302D336165392D346332312D613031322D3038616135666137626536377D"flag{ec8b2ee0-3ae9-4c21-a012-08aa5fa7be67}
二进制转字符串:
可以有空格,也可以无空格
00101111 01100110 00110110 00110111 00110011 00110010 00110100 00110001 00110000 01100001 01100001 01100100 01100011 00110000 00110011 00110111 01100110 01100010 00110000 01100011 01100010 01100001 01100001 00110000 00110000 01100011 00110111 00110101 00110011 00110001 00110011 00110111 00110011 00101110 01110100 01111000 01110100
?
12
bintostr.py -b "00101111 01100110 00110110 00110111 00110011 00110010 00110100 00110001 00110000 01100001 01100001 01100100 01100011 00110000 00110011 00110111 01100110 01100010 00110000 01100011 01100010 01100001 01100001 00110000 00110000 01100011 00110111 00110101 00110011 00110001 00110011 00110111 00110011 00101110 01110100 01111000 01110100"/f6732410aadc037fb0cbaa00c7531373.txt
ascii转字符串
可以是空格分隔,也可以是,分隔
s='45 46 45 46 32 45 32 46 46 45 46 32 46 45 46 46 32 46 46 46 32 45 46 46 46 32 46 46 45 45 46 45 32 45 46 46 46 32 46 46 46 32 46 45 46 46 32'
?
12
bintostr.py -a "45 46 45 46 32 45 32 46 46 45 46 32 46 45 46 46 32 46 46 46 32 45 46 46 46 32 46 46 45 45 46 45 32 45 46 46 46 32 46 46 46 32 46 45 46 46 32"-.-. - ..-. .-.. ... -... ..--.- -... ... .-..
以上实例均来自某些ctf赛题
总结
相关问答
Q1: 用python 怎么把十进制转换成十六进制
#coding=gbkvar=input("请输入十六进制数:")b=bin(int(var,16))print(b[2:])运行结果 详细请参考python自带int函数、bin函数用法 参考网址: class int(x, base=10) Return...
Q2: 串口发送16进制1234,用python接收的是'\x01\x01\x03\x04'但用python再怎么转换成原来的数据1234呢?
1、首先要引用Python的第三方库:serial,打开pycharm。
2、点击file,里面有一个setting。进入setting后点击“progect”下的Project Interpreter,如图。
3、然后在页面中,点击右边的加号,如图。
4、然后在左上角的搜索框搜索pyserial并选中pyserial,如图。
5、最后点击Install Package,然后等上一会就好了。
Q3: python怎样将16进制转化为2进制
#coding=gbk
var=input("请输入十六进制数十六进制函数python:")
b=bin(int(var,16))
print(b[2:])
运行结果
详细请参考python自带int函数、bin函数用法
参考网址十六进制函数python:
class int(x, base=10)
Return an integer object constructed from a number or string x, or return 0 if no arguments are given. If x is a number, return x.__int__(). For floating point numbers, this truncates towards zero.
If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in radix base. 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, with a to z (or A to Z) having values 10 to 35. The default base is 10. The allowed values are 0 and 2–36. Base-2, -8, and -16 literals can be optionally prefixed with 0b/0B, 0o/0O, or 0x/0X, as with integer literals in code. Base 0 means to interpret exactly as a code literal, so that the actual base is 2, 8, 10, or 16, and so that int('010', 0) is not legal, while int('010') is, as well as int('010', 8).
The integer type is described in Numeric Types — int, float, complex.
Changed in version 3.4: If base is not an instance of int and the base object has a base.__index__ method, that method is called to obtain an integer for the base. Previous versions used base.__int__ instead of base.__index__.
Changed in version 3.6: Grouping digits with underscores as in code literals is allowed.
2.bin(x)
Convert an integer number to a binary string. The result is a valid Python expression. If x is not a Python int object, it has to define an __index__() method that returns an integer.
Q4: python中十进制转成十六进制代码
在python中,十进制转换十六进制使用hex()函数。
如:hex(10),十六进制数为0xa
hex(17),十六进制数为0x11
Q5: python将十六进制转为十进制数字的程序怎么写
把十六进制的字串转为十进制数字:
Python代码
int('ff',
16)
255
int('ff',
16)
255
把十进制数字转换为以十六进制表示之字串,可调用内置的hex()函数:
Python代码
hex(255)
0xff
hex(255)
0xff
调用BinAscii模块其中的b2a_hex()函数,可把以ASCII编码的文字以十六进制表示:
Python代码
binascii.b2a_hex('A')
41
binascii.b2a_hex('A')
41
反之也可把以十六进制表示的文字,换成以ASCII编码的文字:
Python代码
binascii.a2b_hex('41')
“A”
十六进制函数python的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、十六进制函数python的信息别忘了在本站进行查找喔。








