Just a c + + class?? I went to look at CodePudding user response:
But the title says "write C program... ", then use the if or switch to judge the specific data type, 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:
#include Char * Bits (char type, int value, char * buf) { Unsigned int bit; The unsigned int u; int len; int i; If (=='c' type) { Bit=0 x80; Len=8; } else if (' s'==type) { Bit=0 x8000; Len=16; } else if (' I '==type) { Bit=0 x80000000; Len=32; } U=(unsigned int) value; For (I=0; I & lt; Len. I++) { If (u & amp; Bit) { Buf [I]='1'; } else { Buf [I]='0'; } Bit & gt;>=1; } Buf [len]='\ 0'; return buf; } Int The main (int arg c, char * argv []) { Char buf [32 + 1]; Char c=20; Short s=40; Int I=100; Printf (" % d: % s \ n ", c, bits (' c ', c, buf)); Printf (" % d: % s \ n ", s, bits (' s', s, buf)); Printf (" % d: % s \ n ", I, bits (' I, I, buf)); I=100; Printf (" % d: % s \ n ", I, bits (' I, I, buf)); return 0; } CodePudding user response:
/* o */ /* 20:00010100 40:0000000000101000 100:00000000000000000000000001100100 11111111111111111111111110011100-100: */ CodePudding user response:
refer to 7th floor mymtom response: /* o */ /* 20:00010100 40:0000000000101000 100:00000000000000000000000001100100 11111111111111111111111110011100-100: */ Output is not directly answer with the program to calculate the answer CodePudding user response:
# include & lt; Stdio. H> #include Int main () { int i; Char b [33]; Char ln [80]. char c; While (1) { The rewind (stdin); The fgets (ln, 80, stdin); If (ln [0]=='\ n') break; If (==2 sscanf (ln, c=% d % c, & amp; I, & amp; C) & amp; & C=='\ n' & amp; & - 128 & lt;=c & amp; & C<{ =127)Ltoa (I, b, 2); Printf (" % 0 * s \ n ", 8, b); } the else If (==2 sscanf (ln, "c" s=% d %, & amp; I, & amp; C) & amp; & C=='\ n' & amp; & - 32768 & amp; & C<{ =32767)Ltoa (I, b, 2); Printf (" % 0 * s \ n ", 16, b); } the else If (==2 sscanf (ln, "I=% d % c", & amp; I, & amp; C) & amp; & C=='\ n') { Ltoa (I, b, 2); Printf (" % 0 * s \ n ", 32, b); nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull