Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize binary conversion in Python

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to achieve binary conversion in Python". The editor shows you the operation process through an actual case. The method of operation is simple, fast and practical. I hope this article "how to achieve binary conversion in Python" can help you solve the problem.

Use int () to transform

The int () class converts a number or string to an integer by calling:

Int (x, base=10) convert any character number to decimal: > int ('55') # string converted to decimal integer 55 > > int (0b11) # binary to decimal 3 > int (0o11) # octal to decimal 9 > int (0x11) # hexadecimal to decimal 1716 to decimal: int (xPol 16)

X is the passed parameter, such as:

> int ('aaaa',16) 43690 > int (' abcdef',16) convert 112593758 to 10: int (xmem8)

X is the passed parameter, such as:

> int ('12345670 recording 8) 2739128 > int (' 105th recorder 8) convert binary 82 to decimal: int (xmem2)

X is the passed parameter, such as:

> int 2 > int 3 > int 4 uses bin (x), oct (x) or hex (x) conversion:

The parameters passed in can be in hexadecimal, and the output is in the form of a string.

Bin (x) to binary: > > bin (0b100) # binary to binary '0b100' > > bin (0o11) # octal to binary' 0b1001' > > bin (11) # decimal to binary '0b1011' > > bin (0xa1) # hexadecimal to binary '0b10100001'oct (x) to octal: > > oct (0b11) # binary to octal Binary '0o3' > > oct (0o77) # octal to octal '0o77' > > oct (55) # decimal to octal '0o67' > > oct (0xab) # hexadecimal to octal '0o253'hex (x) to hexadecimal: > > hex (0b11) # binary to hexadecimal' 0x3'> > hex (0o11) # Octal to hexadecimal '0x9' > hex (11) # Decimal to hexadecimal '0xb' > hex (0xaa) # hexadecimal to hexadecimal' 0xaaa 'using format () conversion

Format (x _ camera _ binary') # converts x to binary

Format (x _ camera _ octal) # converts x to octal

Format (x _ ref _ d') # converts x to decimal

Format (x _ camera _ x') # converts x to hexadecimal

X can be binary, octal, decimal, hexadecimal

Conversion of format (xquotation bookb') to binary > format (0b11gramb') # binary to binary '11' > > format (0o11grab') # octet to binary' 1001' > format (11pint accounb') # decimal to binary '1011' > format (0x11bookbookbookb') # hexadecimal to binary' 10001'format (xmatheo') to octal > format (0b11) 'o') # convert binary to octal' 3' > format (0o11gramme') # convert octal to octal '11' > format (11pencil') # convert decimal to octal'13'> format (0x11recoveringo') # convert hexadecimal to octal '21'format (xmemedd') convert to decimal > format (0b11) 'd') # convert binary to decimal' 3' > format (0o11grammatical') # convert octal to decimal'9' > > format (11d`) # convert decimal to decimal '11' > format (0x11gramd') # convert hexadecimal to decimal' 17'format (xpencil') convert to hexadecimal > format (0b11) 'x') # conversion from binary to hexadecimal' 3' > format (0o11recoveryx') # conversion from octal to hexadecimal'9' > > format (11pencil') # conversion from decimal to hexadecimal'b' > > format (0x11) # conversion from hexadecimal to hexadecimal '11' that's all about "how to convert Python to hexadecimal" Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 202

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report