Python练习5--基础变换

 

十六进制字符串转字节码

Python3:

byte = bytes.fromhex("12acaa")
print (byte)

 

import binascii
n = '1234'
num = binascii.a2b_hex(n)
print (num)

 

Python2:

byte = "abcd12".decode("hex")
print byte

 

ASCII转字节码

Python3:

str_s = "CTF{Hell0_W0r1d}"
str_b = str_s.encode('ascii')

 

通用:

str_s = "CTF{Hell0_W0r1d}"
str_b = str.encode(str_s)

 

整型转字节码

Python3:

num = int(input())
num = hex(num)[2:]
byte = bytes.fromhex(num)
print (byte)

 

Python2:

num = int(input())
num = hex(num)[2:]
if len(num) % 2 == 1:
    num  = '0' + num
byte = num.decode("hex")
print byte

 

字节码转十六进制字符串

Python3:

byte = b"\x124Vx\x9a\xbc\xde"
num = ''.join(["%02x"% x for x in byte])
print (num)

 

byte = b"\x124Vx\x9a\xbc\xde"
num = byte.hex()
print (num)

 

Python2:

byte = b"\x124Vx\x9a\xbc\xde"
num = byte.encode('hex')
print num

 

字节码转ASCII:

Python2:

str_s = b"CTF{Hell0_W0r1d}"
str_b = bytes.decode(str_s)
print str_b

 

十六进制转ASCII码

Python3:

import binascii
num = 89495858189848068103026867923271640189
hex_num = hex(num)[2:]
str_s = binascii.a2b_hex(hex_num)
print (str_s)

 

Python2

import binascii
num = 89495858189848068103026867923271640189
hex_num = hex(num)[2:-1]
str_s = binascii.a2b_hex(hex_num)
print (str_s)

 

ASCII转十六进制形式字节码

Python3:

import binascii
str_s = "CTF{Hell0_W0r1d}"
str_b = str_s.encode('ascii')
hex_num = binascii.b2a_hex(str_b)
print (hex_num)

 

通用:

import binascii
str_s = "CTF{Hell0_W0r1d}"
str_b = str.encode(str_s)
hex_num = binascii.b2a_hex(str_b)
posted @ 2020-01-17 21:18  Hk_Mayfly  阅读(192)  评论(0)    收藏  举报