赞
踩
- # coding: utf-8
- import binascii
- '''
- 参考代码1
- mystery = b"\xe5\x88\xab"
- x = mystery.decode('utf-8')
- print(x)
- y = bytearray.fromhex(\xe5\x88\xab).decode()
- print(y)
- '''
-
- '''
- 参考代码2
- var = 1
- while var == 1:
- a = str(input('请输入字符串:\n'))
- b = []
- for i in a:
- c = hex(ord(i))
- print(c)
- # print('转换结果为:\n\n\n{}\n\n'.format(x))
- '''
-
- '''
- 参考代码3
- x = '\x53\x21\x6a'
- y = [eval(hex(ord(c))) for c in x]
- print(y)
- k = bytearray(y)
- z = binascii.b2a_hex(k)
- print(z)
- '''
-
-
- var = 1
- while var == 1:
- a = str(input('请输入字符串:\n'))
- b = []
- for i in a:
- c = i.encode('utf8')
- #print(c)
- y = [eval(hex(j)) for j in c]
- #print(y)
- k = bytearray(y)
- z = str(binascii.b2a_hex(k))
- #print(z)
- z = z.replace('b','',1)
- #print(z)
- b.append(eval(z))
- #print (b)
- p = ''.join(b)
- print('转换结果为:\n\n{}\n'.format(p))
-
-
-
-
-
例:
输入:呵呵123abc!@#
输出:e591b5e591b5313233616263214023
==========================================================
2019-04-18更新:调用binascii库更简单的代码
- import binascii
-
- def main(a):
- return binascii.hexlify(a.encode('utf8')).decode('utf8')
-
-
- if __name__ == '__main__':
- var = 1
- while var == 1:
- a = input('请输入字符串:\n')
- print('转换为utf8的16进制编码,结果为:\n\n{}\n'.format(main(a)))
用之前写的Python3小程序:把连续的16进制UTF8编码转换为字符串检测一下,结果正确。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。