Python binary Conversion

Source: Internet
Author: User

#coding:utf-8 import binascii a = ‘worker‘ #先把worker转换成二进制数据然后在用十六进制表示 b = binascii.b2a_hex(a) print b #与b2a_hex相反 print binascii.a2b_hex(b) #这个功能和b2a_hex()一样 c = binascii.hexlify(a) print c #这个功能和a2b_hex()一样 print binascii.unhexlify(c) ######运行结果###### >>> 776f726b6572 worker 776f726b6572 worker

Python built-in functions:

Hex ():

12345678910111213 #把10进制转整形换成16进制>>> hex(88)‘0x58‘#把浮点型转换成16进制>>> 1.23.hex()‘0x1.3ae147ae147aep+0‘#内置函数hex和binascii.hexlify()的区别就在于,#hex只能接受整形不能接受字符串>>> hex(‘88‘)Traceback (most recent call last):  File "<pyshell#26>", line 1, in <module>    hex(‘88‘)TypeError: hex() argument can‘t be converted to hexbin():ba

Bin (): Converts decimal shaping to binary characters

12345 #把十进制整型转换成二进制>>> bin(88)‘0b1011000‘>>> bin(33)‘0b100001‘

Oct (): Convert decimal to octal characters

12345 #把十进制转换成八进制>>> oct(500)‘0764‘>>> oct(488)‘0750‘

Chr (): Converts a shape into a single character corresponding to the ASCII code table

12345 #把一个整形转换成ASCII码表中对应的单个字符>>> chr(98)‘b‘>>> chr(97)‘a‘

Ord (): In contrast to CHR, converts the characters in the ASCII code table into corresponding shaping

1234 >>> ord(‘b‘)98>>> ord(‘c‘)99

Reprinted from http://blog.51cto.com/zeping/1261876, thanks to bloggers, for personal study only

Python binary Conversion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.