简体   繁体   English

python如何将特殊字符串u'\\ x08'u'\\ x09'u'\\ x03'转换为8 9 3

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

use crypto encrypt string in Go. 在Go中使用加密加密字符串。

PKCS7 Padding for ciphertext, because ciphertext must be a multiple of the block size (16). PKCS7填充密文,因为密文必须是块大小(16)的倍数。

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

Python decode: Python解码:

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

now, can not get u'\\x02' to number 2. 现在,无法将u'\\ x02'设置为数字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. 我敢肯定这不是在python中执行此操作的最佳方法,但我倾向于在C语言中进行思考,因此请继续。 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 how to store bytes like b'PK\x03\x04\x14\x00\x08\x08\x08\x009bwR\x00\x00\x00\x00\x00\x00\x00 to dataframe or csv in python - how to store bytes like b'PK\x03\x04\x14\x00\x08\x08\x08\x009bwR\x00\x00\x00\x00\x00\x00\x00 to dataframe or csv in python 如何将“ \\ x09”转换回选项卡 - How to convert “\x09” back to tab 如何将退格\\ x08应用于字符串? - How to apply backspace \x08 to a string? 如何在python中将u'\\ x96'转换为u'–' - How to convert u'\x96' to u'–' in python 字符串编码中的Python-3和\\ x Vs \\ u Vs \\ U及其原因 - Python-3 and \x Vs \u Vs \U in string encoding and why Python:将字符串转换为压缩十六进制('01020304' - >'\\ x01 \\ x02 \\ x03 \\ x04') - Python: convert string to packed hex ( '01020304' -> '\x01\x02\x03\x04' ) 如何使Python 2.x Unicode字符串不打印为u'string'? - How to make Python 2.x Unicode strings not print as u'string'? Python Pyodbc Binary Column返回\\ xda \\ x08 \\ xcd \\ x08 \\ xba \\ x08而不是数字 - Python Pyodbc Binary Column is returning \xda\x08\xcd\x08\xba\x08 instead of numbers 原因:python字符串赋值意外地将'\\ b'更改为'\\ x08'而将'\\ a'更改为'\\ x07',为什么Python会这样做? - the reason: python string assignments accidentally change '\b' into '\x08' and '\a' into '\x07', why Python did this? 打印“ \\ x09”,打印0x20,如何将0x09打印到STDOUT? - Print “\x09”, print 0x20, how to print 0x09 to STDOUT?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM