Home > Back-end >  C: binary number is converted to a decimal number
C: binary number is converted to a decimal number

Time:03-10

Title description:
First enter a n bit binary number, high input, then output the corresponding decimal number, for example, enter the following
5
11011
5 said binary number five, 11011 is a binary number, the output of
Value of binary sequence (11011) : 27

Enter
5
11011

O
Value of binary sequence (11011) : 27

The sample input
5
11011

Sample output
Value of binary sequence (11011) : 27

Procedure is as follows:
#include
Void main ()
{
Int n, TWN, ten=0;
Int a, b, c, d, e;
The scanf (" % d % d ", & amp; N, & amp; TWN);

If (n> 4)
{
A=TWN/10000;
B=TWN % 10000/1000;
C=1000/100 TWN %;
D=TWN % 100/10;
E=TWN % 10;
Ten=a * 16 + b * 8 + c * * 2 + 4 + d e;
}
Else if (n> 3)
{
A=TWN/1000;
B=TWN % 1000/100;
C=100/10 TWN %;
D=TWN % 10;
Ten=a * b * 8 + 4 * 2 + c + d;
}
Else if (n> 2)
{
A=TWN/100;
B=TWN % 100/10;
C=TWN % 10;
Ten=a * b * 4 + 2 + c;
}
Else if (n> 1)
{
A=TWN/10;
B=TWN % 10;
Ten=a * 2 + b;
}
Else if (n> 0)
{
Ten=TWN;
}

Printf (" binary sequence (% d) value is: % d \ n ", TWN, ten);
}

CodePudding user response:

When the input more than five, calculation error, modify as follows, for reference:
 # include & lt; Stdio. H> 
Void main ()
{
Int n, TWN, ten=0, v=1;//has a weight
Int a, b, c, d, e;
The scanf (" % d % d ", & amp; N, & amp; TWN);
B=TWN;
While (n -) {
A=b % 10;
Ten +=a * v.
V *=2;
B/=10;
}
/* if (n> 4)
{
A=TWN/10000;
B=TWN % 10000/1000;
C=1000/100 TWN %;
D=TWN % 100/10;
E=TWN % 10;
Ten=a * 16 + b * 8 + c * * 2 + 4 + d e;
}
Else if (n> 3)
{
A=TWN/1000;
B=TWN % 1000/100;
C=100/10 TWN %;
D=TWN % 10;
Ten=a * b * 8 + 4 * 2 + c + d;
}
Else if (n> 2)
{
A=TWN/100;
B=TWN % 100/10;
C=TWN % 10;
Ten=a * b * 4 + 2 + c;
}
Else if (n> 1)
{
A=TWN/10;
B=TWN % 10;
Ten=a * 2 + b;
}
Else if (n> 0)
{
Ten=TWN;
} */
Printf (" binary sequence (% d) value is: % d \ n ", TWN, ten);

}

CodePudding user response:

Fyi:
 # include & lt; Stdio. H> 
#include
#include
#include
Int main () {
Int I, v;
Char bs [33].
Char b [33];
Char hs [9].
Char h [9];
Char s [4];
Char * e;

//decimal integer binary string;
I=1024;
Ltoa (I, b, 2);
Sprintf (bs, "% 032 s", b);
Printf (" I=% d, bs=% s \ n ", I, bs);
//decimal integer hexadecimal string;
I=1024;
Ltoa (I, h, 16);
Sprintf (hs, "% s" 08, h);
Printf (" I=% d, hs=% s \ n ", I, hs);
//hex string converted to a decimal number
Strcpy (hs, "00000400");
Sscanf (hs, "% x", & amp; I);
Printf (" the hs=% s, I=% d \ n ", the hs, I);
//binary string into hexadecimal string;
Strcpy (bs, "00000000000000000000010000000000");
I=strtol (bs, & amp; E, 2);
Ltoa (I, h, 16);
Sprintf (hs, "% s" 08, h);
Printf (" bs=% s, hs=% s \ n ", bs, hs);
//binary string converted to a decimal number;
Strcpy (bs, "00000000000000000000010000000000");
I=strtol (bs, & amp; E, 2);
Printf (" bs=% s, I=% d \ n ", bs, I);
//hex string into a binary string
Strcpy (hs, "00000400");
Sscanf (hs, "% x", & amp; I);
Ltoa (I, b, 2);
Sprintf (bs, "% 032 s", b);
Printf (" the hs=% s, bs=% s \ n ", hs, bs);
//ASC \ GBK string hexadecimal string
Strcpy (s, "a han");
I=0;
While (1) {
If (0==s [I]) break;
Sprintf (hs + I * 2, 02 x %, (unsigned char) s [I]);
i++;
}
The setlocale (LC_ALL, "CHS");
Printf (" s=% s, hs=% s \ n ", s, hs);
//hex string into (GBK Chinese characters) and characters (ASC)
Strcpy (hs, "61 baba");
I=0;
While (1) {
If (1!=sscanf (hs + I * 2, "% 2 x", & amp; V)) break;
S [I]=(char) v.
i++;
}
S [I]=0;
Printf (" the hs=% s, s=% s \ n ", hs, s);

return 0;

}
//I=1024, bs=00000000000000000000010000000000
//I=1024, hs=00000400
//hs=00000400, I=1024
//bs=00000000000000000000010000000000, hs=00000400
//bs=00000000000000000000010000000000, I=1024
//hs=00000400, bs=00000000000000000000010000000000
//s=a han, hs=61 baba
//hs=61 baba, s=a han

CodePudding user response:

If you don't call ready-made functions to write their own words to write functional, this example is arbitrary hexadecimal,



Hex: fa +=x * power (16, c + +);
  • Related