当前位置:网站首页>[C language] convert decimal numbers to binary numbers
[C language] convert decimal numbers to binary numbers
2022-07-02 12:06:00 【Study silently every day】
Preface
OK, Today, let's talk about a simple code , This code is simple for program apes , But for learning C Language is more complicated for students , Share it now , Learn with you .
One 、 Topic explanation
The title requires :
- Put one [0-1023] Integers in the range are converted to unsigned binary numbers
- If it exceeds the value or the input is a non integer, it will exit normally , Otherwise, loop conversion
I will directly share the operation interface with you , Let's first look at the interface , You should know the logical relationship of the code , Or function , Then share the code :
1: The value entered is out of range

As shown in the figure , Input 234, Output 11101010; If we type 3333, The exit , because 3333 More than the 1023
2: The input is a letter or not a character

such as , Input 23, Output 10111, Input 222, Output 11011110, If input dc34, The exit
Two 、 analysis
1. Code
The code is as follows ( Example ):
Let's learn the code together :
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
void decToBin(int n)// Enter a number 15
{
int result = 0;// result
int k = 1;
int i = 0;
while(n)// Divide to 0 Just stop
{
i = n % 2; // remainder i=1 //i=1 //i=1 //i=1
result = k * i + result; //result=1 //result=11 //result=111 //result=1111
k = k * 10; //k=10 //k=100 //k=1000
n = n / 2; //n=7 //n=3 //n=1 //n=0 sign out
}
printf(" The binary number we get is :%d \n",result);//1111
}
int main() {
char s[6];
char *p;
do{
printf(" Please enter a [0-1023] Range of integers :") ;
gets(s);
p=s;
int Num=1,j=0,Num_T=0,Totle=0;
j=strlen(s);
while(*p!='\0') // Enter an integer number
{
if(*p<'9'&&*p>'0'){
for(int k=1;k<j;k++)
Num=Num*10;
// printf("Num=%d\n",Num);
Num_T=(*p-'0')*Num;
//printf("%c\n",*p);
Totle=Totle+Num_T;
// printf("Totle=%d\n",Totle);
Num=1;
j--;
p++;
}
else
exit(0);
}
if(Totle>=0&&Totle<1024)
decToBin(Totle);
else
exit(0);
}while(1);
}2. Key algorithms
1: Decimal to binary direct code :
while(n)// Divide to 0 Just stop
{
i = n % 2; // remainder i=1 //i=1 //i=1 //i=1
result = k * i + result; //result=1 //result=11 //result=111 //result=1111
k = k * 10; //k=10 //k=100 //k=1000
n = n / 2; //n=7 //n=3 //n=1 //n=0 sign out
}2. Convert characters to decimal numbers , If the input is not 0-9 Characters between automatically exit :.
while(*p!='\0') // Enter an integer number
{
if(*p<'9'&&*p>'0'){
for(int k=1;k<j;k++)
Num=Num*10;
// printf("Num=%d\n",Num);
Num_T=(*p-'0')*Num;
//printf("%c\n",*p);
Totle=Totle+Num_T;
// printf("Totle=%d\n",Totle);
Num=1;
j--;
p++;
}
else
exit(0);
}3. Judge if the value is 0-1023, Conversion , If not , What we judge here is that the value exceeds 1023, Quit
if(Totle>=0&&Totle<1024)
decToBin(Totle);
else
exit(0);summary
Here is a summary of the article :
That's what we're going to talk about today , This paper simply introduces the function and key algorithm of the program , I hope everyone is happy to learn .
边栏推荐
猜你喜欢

jenkins 凭证管理

YYGH-9-预约下单

【2022 ACTF-wp】

Develop scalable contracts based on hardhat and openzeppelin (I)

SVO2系列之深度滤波DepthFilter

Pyqt5+opencv project practice: microcirculator pictures, video recording and manual comparison software (with source code)

排序---

HOW TO ADD P-VALUES TO GGPLOT FACETS

PyTorch nn. Full analysis of RNN parameters

HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
随机推荐
to_ Bytes and from_ Bytes simple example
SCM power supply
Time format display
Small guide for rapid formation of manipulator (VII): description method of position and posture of manipulator
自然语言处理系列(二)——使用RNN搭建字符级语言模型
【2022 ACTF-wp】
通讯录的实现(文件版本)
Log4j2
Deep understanding of NN in pytorch Embedding
How to Create a Beautiful Plots in R with Summary Statistics Labels
Leetcode122 the best time to buy and sell stocks II
[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)
On data preprocessing in sklearn
qt 仪表自定义控件
Jenkins用户权限管理
Natural language processing series (I) -- RNN Foundation
Dynamic debugging of multi file program x32dbg
H5,为页面添加遮罩层,实现类似于点击右上角在浏览器中打开
Leetcode922 按奇偶排序数组 II
b格高且好看的代码片段分享图片生成