当前位置:网站首页>[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 .
边栏推荐
- On data preprocessing in sklearn
- Applet link generation
- YYGH-BUG-04
- How does Premiere (PR) import the preset mogrt template?
- The most understandable f-string tutorial in history, collecting this one is enough
- Industry analysis
- Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
- to_bytes与from_bytes简单示例
- Leetcode14 最长公共前缀
- 全链路压测
猜你喜欢
![[geek challenge 2019] upload](/img/04/731323142161a4994c14fedae38b81.jpg)
[geek challenge 2019] upload

测试左移和右移

Deep understanding of NN in pytorch Embedding

How to Easily Create Barplots with Error Bars in R

xss-labs-master靶场环境搭建与1-6关解题思路

Natural language processing series (II) -- building character level language model using RNN

mysql表的增删改查(进阶)

Map和Set

conda常用命令汇总

HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
随机推荐
Mish-撼动深度学习ReLU激活函数的新继任者
浅谈sklearn中的数据预处理
mysql索引和事务
Dynamic debugging of multi file program x32dbg
Dynamic memory (advanced 4)
MySQL stored procedure cursor traversal result set
Natural language processing series (III) -- LSTM
MSI announced that its motherboard products will cancel all paper accessories
Jenkins用户权限管理
HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R
Repeat, tile and repeat in pytorch_ The difference between interleave
数据分析 - matplotlib示例代码
HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
GGPlot Examples Best Reference
求16以内正整数的阶乘,也就是n的阶层(0=<n<=16)。输入1111退出。
Gaode map test case
PHP 2D and multidimensional arrays are out of order, PHP_ PHP scrambles a simple example of a two-dimensional array and a multi-dimensional array. The shuffle function in PHP can only scramble one-dim
FLESH-DECT(MedIA 2021)——一个material decomposition的观点
How to Create a Beautiful Plots in R with Summary Statistics Labels
Those logs in MySQL