A talk C Chestnut bar (seventh time: C-language example---binary conversion)

Source: Internet
Author: User


Ladies and gentlemen, crossing. Hello everyone, start today. We talk about large couplet sci-tech fiction: C chestnuts, that is, C language examples.

Gossip Hugh,

Words return to the positive turn. Let's talk C chestnuts together!



Crossing, they are. The last time we were talking about a sample that generated a random number. This time we say the example is: the conversion of the binary. Speaking of the system, we are expecting

are not unfamiliar. The binary in the computer is binary. octal, decimal, hexadecimal.


What we're going to say today is how decimal is converted into binary. Octal and hexadecimal. Here I take the steps and algorithmic thinking of knocking the code

Write it for everyone to take a test.

1. Get the values that the user wants to convert and think about the binary of the conversion. Here to check if the input is correct. This is the embodiment of the robustness of the program.

2. Different conversions are made according to the different input of the user. In the code, I encapsulate the conversion process into a function.

3. Output the converted results. The output converted results are also encapsulated as a function.


Crossing, they are. Let's talk about how to make a binary conversion. That is, the contents of the binary conversion function.

1. Convert the binary using the converted numerical mode. and save the results of the mold. For example, the converted value is 9 and the conversion is 2.

Then the result of the =9%2. This step is run multiple times. The number of details is determined by step 2.

Turn the results of each run from left to right

The result of converting into a row is the conversion. Use an array in your program to save the results.


2. Use the converted value divided by the converted binary. and whether the inferred quotient is 0. Assume that 0 steps 1 and 2 are not repeated until the quotient equals 0.


Crossing, they are. I'm done with the conversion function. Let's talk about the function of the output input conversion result.


1. The output is the opposite of the order in which the results are saved in the conversion transformation function. Save is in the direction of the array head to tail, when output

is the direction of the head along the end of the array.

Two in the opposite direction.


2. The output of the 16 binary includes a-f these characters. So they were specifically converted.


Crossing, the text does not write code, the specific code put in my resources, you can click the link to download the use.


You crossing. We'll talk about this in the example of a binary conversion.

See what else is in the back. and listen to tell.

A talk C Chestnut bar (seventh time: C-language example---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.