encode和decode的妙用

做网络编程的时候,经常需要把接收到的数据用16进制的方式打印出来,方便查看。今天发现在Python下有这样一个简单的方法。

 

>>> "hello".encode("hex")
'68656c6c6f'

 

相应的还可以

>>> '68656c6c6f'.decode("hex")
'hello'

 

查了一下手册,还有这些codec可用

Codec

Aliases

Operand type

Purpose

base64_codecbase64, base-64byte stringConvert operand to MIME base64
bz2_codecbz2byte stringCompress the operand using bz2
hex_codechexbyte stringConvert operand to hexadecimal representation, with two digits per byte
idna Unicode stringImplements RFC 3490. New in version 2.3. See also encodings.idna
mbcsdbcsUnicode stringWindows only: Encode operand according to the ANSI codepage (CP_ACP)
palmos Unicode stringEncoding of PalmOS 3.5
punycode Unicode stringImplements RFC 3492. New in version 2.3.
quopri_codecquopri, quoted-printable, quotedprintablebyte stringConvert operand to MIME quoted printable
raw_unicode_escape Unicode stringProduce a string that is suitable as raw Unicode literal in python source code
rot_13rot13Unicode stringReturns the Caesar-cypher encryption of the operand
string_escape byte stringProduce a string that is suitable as string literal in python source code
undefined anyRaise an exception for all conversions. Can be used as the system encoding if no automatic coercion between byte and Unicode strings is desired.
unicode_escape Unicode stringProduce a string that is suitable as Unicode literal in python source code
unicode_internal Unicode stringReturn the internal representation of the operand
uu_codecuubyte stringConvert the operand using uuencode
zlib_codeczip, zlibbyte stringCompress the operand using gzip
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值