關(guān)于十六進制函數(shù)python的信息

關(guān)于python如何實現(xiàn)各進制轉(zhuǎn)換的總結(jié)大全

ctf經(jīng)常遇到進制轉(zhuǎn)換的問題,就正好做一個進制轉(zhuǎn)換總結(jié),分享出來供大家參考學(xué)習(xí),下面來一起看看詳細的介紹:

惠濟網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站等網(wǎng)站項目制作,到程序開發(fā),運營維護。創(chuàng)新互聯(lián)公司于2013年成立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司。

字符串與十六進制轉(zhuǎn)換

例如百度ctf 12月的第二場第一個misc

?

1

666C61677B65633862326565302D336165392D346332312D613031322D3038616135666137626536377D

比較簡單的一種做法就是直接調(diào)用字符串的.decode('hex')解密即可, 但如果不用這個函數(shù)你會怎么解呢?

一種思路就是先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')函數(shù),另外還有兩個轉(zhuǎn)16進制的函數(shù),這里都總結(jié)一下

內(nèi)置函數(shù)hex()

只能轉(zhuǎn)換10進制整數(shù)為十六進制,不能轉(zhuǎn)字符串

binascii庫的hexlify()和b2a_hex()

這兩個函數(shù)的功能是將字符串轉(zhuǎn)換成十六進制,對應(yīng)的解密函數(shù)分別為 unhexlify()和a2b_hex()

進制互轉(zhuǎn)

二進制,八進制,十六進制轉(zhuǎn)10進制比較簡單,直接調(diào)用

int函數(shù)

?

1

int(str,base) //返回十進制整數(shù),但注意此時第一個參數(shù)為字符串

對應(yīng)的解密函數(shù)分別是

?

12345

bin() //10進制轉(zhuǎn)二進制 oct() //十進制轉(zhuǎn)八進制 hex() //十進制轉(zhuǎn)十六進制

但二進制直接轉(zhuǎn)16進制就需要多走一步了,先用int轉(zhuǎn)十進制,在用上面提到的hex()函數(shù)將十進制轉(zhuǎn)換成十六進制,比較精簡的寫法是

?

1

map(lambda x:hex(int(x,2)),['0011']) //lambda表達式

或者是

?

1

[hex(int(x,2)) for x in ['0011']] //列表解析

對應(yīng)的解密函數(shù)就是

?

1

map(lambda x:bin(int(x,16)),['ef'])

最后在附上自己用python寫的一個進制轉(zhuǎn)換小工具,主要功能是對一組二進制,或者ascii,或十六進制轉(zhuǎn)換成字符串,想必ctf上也經(jīng)常會遇到這類題型吧

?

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) #批量二進制轉(zhuǎn)十進制 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

用法:

十六進制轉(zhuǎn)字符串:

666C61677B65633862326565302D336165392D346332312D613031322D3038616135666137626536377D

?

12

bintostr.py -x "666C61677B65633862326565302D336165392D346332312D613031322D3038616135666137626536377D"flag{ec8b2ee0-3ae9-4c21-a012-08aa5fa7be67}

二進制轉(zhuǎn)字符串:

可以有空格,也可以無空格

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轉(zhuǎn)字符串

可以是空格分隔,也可以是,分隔

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賽題

總結(jié)

python將十六進制轉(zhuǎn)為十進制數(shù)字的程序怎么寫

把十六進制的字串轉(zhuǎn)為十進制數(shù)字:

Python代碼

print int('ff', 16)

255

print int('ff', 16)

255

把十進制數(shù)字轉(zhuǎn)換為以十六進制表示之字串,可調(diào)用內(nèi)置的hex()函數(shù):

Python代碼

print hex(255)

0xff

print hex(255)

0xff

調(diào)用BinAscii模塊其中的b2a_hex()函數(shù),可把以ASCII編碼的文字以十六進制表示:

Python代碼

print binascii.b2a_hex('A')

41

print binascii.b2a_hex('A')

41

反之也可把以十六進制表示的文字,換成以ASCII編碼的文字:

Python代碼

print binascii.a2b_hex('41')

“A”

用python 怎么把十進制轉(zhuǎn)換成十六進制

#coding=gbkvar=input("請輸入十六進制數(shù):")b=bin(int(var,16))print(b[2:])運行結(jié)果 詳細請參考python自帶int函數(shù)、bin函數(shù)用法 參考網(wǎng)址: class int(x, base=10) Return...

python中十進制轉(zhuǎn)成十六進制代碼

在python中,十進制轉(zhuǎn)換十六進制使用hex()函數(shù)。

如:hex(10),十六進制數(shù)為0xa

hex(17),十六進制數(shù)為0x11

python怎樣將16進制轉(zhuǎn)化為2進制

#coding=gbk

var=input("請輸入十六進制數(shù):")

b=bin(int(var,16))

print(b[2:])

運行結(jié)果

詳細請參考python自帶int函數(shù)、bin函數(shù)用法

參考網(wǎng)址:

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.

python怎么輸入16進制數(shù)

a='0x0012e' b= hex(eval(a)) print b 輸出 0x12e 注意,一般計算機的十六進制數(shù)直接輸出的時候是不補0的,所以 0x12e 就是 0x0012e,就好像 0005和5在整型數(shù)是存儲成一樣的值。

新聞標(biāo)題:關(guān)于十六進制函數(shù)python的信息
分享網(wǎng)址:http://muchs.cn/article40/dossieo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計公司、企業(yè)網(wǎng)站制作、虛擬主機關(guān)鍵詞優(yōu)化、ChatGPT全網(wǎng)營銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)