This program will take 1 command-line input in the form of an 8-bit number. It w
ID: 3834697 • Letter: T
Question
This program will take 1 command-line input in the form of an 8-bit number. It will convert that 8-bit BINARY number into a DECIMAL number. For example, if the user runs the program with a command-line argument of 00000101, the result is the decimal number 5 If the user enters no command-line inputs, then the program will print out an error message. If the user enters more than 1 command-line input, the program will print out that same error message. If the user enters an input that is NOT comprised of an 8-bit binary number, the program will print out that same error message (the program only needs to check the length of that input, not whether the user actually put in a proper binary number). See example print out (note that the first 2 entries are purposeful mistakes and generate the error message) ./8binconv Usage 8binconvExplanation / Answer
#include<stdio.h>
#include<string.h>
#include<math.h>
void main(int argc, char* argv[]) {
int i, sum = 0;
if (argc != 2) { // checks if there are valid number of arguments
printf("Usage: 8binConv <8-bit binary number> ");
return;
}
else if (strlen(argv[1]) != 8) { //checks if the length of argument is 8
printf("Usage: 8binConv <8-bit binary number> ");
return;
}
for (i = 0; i < 8; i ++) // performs binary to decimal conversation
sum += pow(2,7-i)*(argv[1][i]-'0');
printf("That's %d in decimal ", sum); //print the result
}
I tried my best to keep the code as simple as possible. I followed the exact same operation as described in the question. If you need any help regarding this or if you are having trouble understanding this, please feel free to comment below. I shall try my best to solve your issue.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.