# A summary of python's implementation of each base conversion

• 2020-06-07 04:42:33
• OfStack

preface

When playing ctf, I often encounter the problem of base conversion, so I just make a summary of the base conversion and share it for your reference and study. Let's start with 1 and have a look at the detailed introduction:

Convert string to base 106

For example, Baidu ctf's first misc in The second game in December

``````
666C61677B65633862326565302D336165392D346332312D613031322D3038616135666137626536377D``````

The simpler one is to call the string directly `.decode('hex')` Decrypt it, but how would you solve it without using this function?

The first idea is to solve the ascii value of each group in two groups, and then merge the strings to get the specific code as follows

``````
import re
s='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}``````

I said string `decode('hex')` The function, and then there are two other functions that rotate hexadecimal, so I'm going to summarize them both here by 1

The built-in function hex()

You can only convert decimal integers to decimal 106. You cannot convert strings

hexlify() and b2a_hex() of the binascii library

The function of these two functions is to convert the string to base 106, and the corresponding decryption function is respectively `unhexlify()` and `a2b_hex()`

2 base, 8 base, 106 to 10 base is relatively simple, direct call

int function

``````
int(str,base) // return 10 Hexadecimal integer , But notice what happens here 1 The parameters are strings ``````

The corresponding decryption functions are

``````
bin() //10 Turn into the system 2 Into the system

oct() //10 Turn into the system 8 Into the system

hex() //10 Turn into the system 106 Into the system ``````

But if you go straight from base 2 to hexadecimal, you'll have to take an extra step, using int to go straight to base 10 `hex()` The function converts base 10 to base 106

``````
map(lambda x:hex(int(x,2)),['0011']) //lambda expression ``````

Or is it

``````
[hex(int(x,2)) for x in ['0011']] // List of analytical ``````

The corresponding decryption function is

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

Finally, I attached a hexadecimal conversion widget written by python, whose main function is to convert a group of 2, ascii, or 106 into a string. Presumably, this type of question is often encountered on ctf

``````
# make by  jiang sir
#coding:utf-8
import re
import argparse

def bintostr(text):
text = text.replace(' ','')
text = re.findall(r'.{8}',text)
s = map(lambda x:chr(int(x,2)),text) # batch 2 Turn into the system 10 Into the system
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()
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``````

Usage:

666C61677B65633862326565302D336165392D346332312D613031322D3038616135666137626536377D

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

It can have Spaces or no Spaces

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

``````
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"

ascii turns strings

It can be a space delimiter, or it can be a space delimiter

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'

``````
import re
s='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}``````
0

The above examples are all from some ctf questions

conclusion

Related articles: