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

The method of Python binary conversion and ASCLL conversion

2025-01-14 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 "Python base conversion and ASCLL conversion method". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "Python base conversion and ASCLL conversion method" can help you solve the problem.

Binary conversion

The conversion between binary systems is mainly done by decimal system. In the process of binary conversion, you can first convert the relevant binary to decimal, and then carry out secondary conversion to achieve the desired effect. Of course, in the binary conversion, it is also possible to convert directly without the decimal transfer operation.

1. Decimal and binary 1.1 Decimal to binary

Use the bin () function

Bin (number, /) converts other binary to binary and returns the binary representation of the integer.

N = 120print (bin (n)) # n does not change # 0b1111000

The binary starts with 0b. If you want to remove the 0b display, you can use the slicing method.

Print (bin (n) [2:]) # 11110001.2 binary to decimal

Use the int () function

N = 120er = bin (n) print (int (er, 2)) # 1202. Decimal and octal 2.1 decimal to octal

Use the oct () function

N = 120print (oct (n)) # 0o170

2.2 Octal to decimal

Use the int () function

N = 120eight = oct (n) print (int (eight,8)) # 1203. Decimal and hexadecimal 3.1 decimal to hexadecimal

Use the hex () function

N = 120print (hex (n)) # 0x783.2 hexadecimal to decimal

Use the int () function

N = 120sixteen = hex (n) print (int (sixteen, 16)) # 1204. Conversion between other bases

Bin (), oct (), hex () functions can be used for direct conversion between binary, in which case the prefix of the binary must be taken.

For example:

Binary to octal

1. Binary to decimal and then octal

N = 120er = bin (n) # er = '0b1111000'print (oct (int (er,2) # 0o170

two。 Direct conversion of binary to octal

N = 120er = bin (n) # er = '0b1111000'bb = oct (0b1111000) print (bb) # 0o170ASCll conversion 1. Convert integers to ASCLL characters

Using the chr () function, you can convert integers between [0,255] to ASCLL characters.

For example:

A = chr (65) # a = 'Agg2. Convert ASCLL characters to integers

Use the ord () function to convert an ASCLL character to the corresponding integer.

The function b = ord ('A') print (b) # 65 shows that bin () converts the other binary to binary, returns the binary representation of the integer oct (), converts the other binary to octal, and returns the octal representation of the integer hex () to hexadecimal. Return the hexadecimal representation of the integer int (number, /) convert the other decimal to decimal chr () convert the integer to the corresponding ASCLL character ord () convert an ASCLL character into the corresponding integer on the "Python binary conversion and ASCLL conversion method" content is introduced here, thank you for 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: 0

*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