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

What is the common conversion between binary systems in python?

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "what is the common conversion between binary systems 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 "what is the common conversion between binary systems in python" can help you solve the problem.

1. In many cases, conversion between different binary systems is required.

Among them, the built-in function of python is commonly used for binary conversion. Generally, when using the built-in function for conversion, the string input in the console or the custom string is first converted to decimal and then decimal to other decimal. The common conversion is binary, decimal, octal and hexadecimal. One of the principles to be followed is:

Other systems are converted to decimal using the int function, others to binary using the bin function, others to octal using the oct function, others to hexadecimal to hex functions, and with the help of decimal production as a bridge in the middle, that is, using the int () function

The following table reflects the conversion between common binary systems

And after conversion to the corresponding weight, the corresponding string will have the corresponding prefix, the binary prefix is 0b, the octal prefix is 0o, and the hexadecimal prefix is 0x.

2 octal hexadecimal binary-bin (int (input (), 8) bin (int (input (), 10)) bin (int (input (), 16)) octal oct (int (input (), 2))-oct (int (input (), 10) oct (int (input (), 16)) decimal int (input (), 2) int (input (), 8)-int (input (), 2) hexadecimal hex (int (input ()) 2) hex (int (input (), 8)) hex (int (input (), 10))-

When using the built-in function, you can use a function corresponding to which base it is converted to. You need to convert it to decimal (int () function) first. The built-in functions involved in binary conversion are: 2: bin (), 8: oct (), 10: int (), hexadecimal: hex ()

If _ _ name__ = ='_ main__': # input receives a string. Use the int function to define what binary string is input and convert it into a decimal digit print (bin (int (input (), 16)) print (int (input (), 10)) print (oct (int (input (), 10)) print (hex (int (input (), 10)) 2. The second is to use the format function for conversion.

Add bmemore x to format to convert other binary to binary, octal, or hexadecimal.

If _ _ name__ = ='_ main__': print ("{: B}" .format (int (input (), 8)) print ("{: o}" .format (int (input (), 8) print ("{: X}" .format (int (input (), 8)) 3. Manually convert decimal to other binary codes class Solution: # convert decimal numbers to arbitrary binary (1-16) def decimalToAny (self, decimal: int, x: int): remainder = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C" "D", "E", "F"] # execute loop res = "" while decimal: res = remainder [decimal% x] + res decimal / / = x return res if _ name__ = ='_ main__': decimal, x = map (int, input (). Split ()) print (Solution (). DecimalToAny (decimal) when n is greater than 0 X)) other decimal codes class Solution: # Fast Power: X * * n def quickPower (self, x: int, n: int): res = 1 while n > 0: if n% 2 = 1: res * = x * = x n / / = 2 return res def anyToDecimal (self, s: str Base: int): n = len (s) res = 0 for i in range (n): # number, the ord function gets the ascii value of the letter if "0"

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: 213

*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