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 perform binary conversion in Python

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this article Xiaobian for you to introduce in detail "Python how to carry out binary conversion", the content is detailed, the steps are clear, the details are handled properly, I hope this "Python how to carry out binary conversion" article can help you solve your doubts, following the editor's ideas slowly in-depth, let's learn new knowledge.

I. introduction

Introduction:

When computers exchange data, there is often a process of binary conversion, and we know that computers only recognize 0 and 1. In the memory system, the operation is basically based on binary, but sometimes the data is too large, in order to facilitate storage management, the computer will use hexadecimal to store data, but how to achieve data conversion?

We humans have ten fingers, so we naturally use the decimal system. Every time we count to 10, we start counting again with zero, so every decimal comes like this.

For other binary systems, the same is true, such as the most common binary, that is, every binary, slowly, and so on, so how wonderful the world of mathematics is!

What we bring to you today is to use Python to implement binary conversion, which includes the following:

Dec (decimal)-> bin (binary)

Dec (decimal)-> oct (octal)

Dec (decimal)-> hex (hexadecimal)

Introduction to all kinds of binary systems

Before we convert, let's take a look at the following bases

Decimal (Decimal)

The decimal system as we know it actually starts with zero, and after counting to 9, it jumps to 10, so it becomes 10, and people always count.

Binary (Binary)

In the same way, the binary system starts with 0, which is 00 (the previous 0 can be omitted, but it is retained for better description). By 01, it also becomes 10 [3 in decimal]. Then 11 [4 in decimal], 100 [5 in decimal]. No, no, no. and so on

Because the English word of binary is binary, in the process of computer operation, using binary will use the following methods, 0b11 (4), 0b1101 (13), and so on.

Octal (Octal)

Understand binary, it is easy to understand octal, octal is every octal, the range is 0: 7, compare with binary, it is easy to understand!

Hexadecimal (Hexadecimal)

Hexadecimal may be a little more complex. Hexadecimal is represented by the number 09and the letter A-F (case-free), so A stands for 10 and F for 15. Why is hexadecimal so widely used in the computer field?

When the data is large, binary is obviously less used, and if you look at hexadecimal, it's much shorter.

Hexadecimal can be used in more than that. For example, when the original painter uses RGB tricolor, there will be 256 x 256 x 256 combinations. We can use hexadecimal to convert each color contrast into hexadecimal characters, which will be much more convenient. the following is a very simple C language Mini Program that I wrote when I was adjusting colors before. er, it seems to be a little beside the point. Haha, whatever.

# include#include int main () {printf ("rgb trichromatic conversion to hexadecimal"); int arecrome brecediclage; while (d! = 2) {printf ("enter 1 to continue, enter 2 to exit"); scanf ("% d", & d); scanf ("% d%d%d", Printf ("% x% x% x]", / /% x can directly convert the decimal we entered into hexadecimal} return 0;} III. Use Python code to complete the conversion.

3.1 convert decimal to binary

I don't remember exactly when I first learned binary conversion. the common method of binary conversion is to take the remainder of 2, and then get that the reversal of the remainder is the binary we need, for example, for example, what is the binary of the decimal number 13?

This is also the method that the teacher taught us, and when we first came into contact with the binary conversion, we used the target number num to find the remainder of 2 until the quotient was 0, and then the remainder was output in reverse, and the binary number we obtained for us. Some students will ask, how can I be so sure that this is the binary number corresponding to 13?

The answer is:

Because of the particularity of the binary system, the number of numbers from right to left corresponds to 20 x 1 + 21 x 0 + 22 x 1 + 23 x 1 = 1 + 4 + 8 = 13, giving everyone a shorthand formula. The corresponding values are 20 ~ 210, respectively.

So with ideas, we can happily knock on the code.

# decimal integer to binary def dec_to_bin (num): # function name to see its name and meaning l = [] # create an empty list if num

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