简体   繁体   中英

python how to convert Special string u'\x08' u'\x09' u'\x03' to 8 9 3

use crypto encrypt string in Go.

PKCS7 Padding for ciphertext, because ciphertext must be a multiple of the block size (16).

exa:   ciphertext is : 123456789abcde. (len:14) 
       pandding:  []byte("123456789abcdef") + []byte(2) + []byte(2)

Python decode:

list: [u'1', u'2', u'3', ...,u'd', u'e', u'\x02', u'\x02']

now, can not get u'\\x02' to number 2.

I'm sure this is not the best way to do this in python, but I tend to think in C, so here goes. It is probably pretty bad according to most people, but it gets the job done.

c = u'\x02'
byte = bytearray(c, 'utf-8')[0]
print(chr(ord('0') + byte))
=> 2

Inspired by andars

c = ord(u'\x02') + ord('0')
print chr(c)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM