C Program: Currently I have everything I need up until the last print out line:
ID: 3935485 • Letter: C
Question
C Program:
Currently I have everything I need up until the last print out line: "The CRC observed at the end of the input: ". This program only takes in two argv values the first agrv[1] is either c or v followed by agv[2] which is D2B9CB. I need to somehow write a function that returns the last 3 of the input string both in binary and hexadecimal. HERE IS MY CODE THUS FAR:
//Libraries
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Functional Prototypes
void modeType(char *argv);
void hexConvert(char hex[60]);
void calcPrint(char *argv);
//Polynomial: x^12 + x^11 + x^7 + x^5 + x^4 + x^1 + 1
const char poly[17] = "1100 0101 1001 1";
int main (int argc, char *argv[]) {
//Header print out
printf("CRC Tester by [Your-name] ");
fflush(stdout);
//Error and exit out if argv are not entered correctly
if(argc != 3) {
printf("ERROR - Entered invlaid number of arguments ");
fflush(stdout);
exit(0);
}
//Mode type print out
printf("Mode of operation: ");
fflush(stdout);
modeType(argv[1]);
//Hex value print out
printf("The input string (hex): %s ", argv[2]);
fflush(stdout);
//Binary print out of hex values
if (strlen(argv[2]) >= 3 && strlen(argv[2]) < 41) {
printf("The input string (bin): ");
fflush(stdout);
hexConvert(argv[2]);
printf(" ");
fflush(stdout);
}
else {
printf("ERROR - Invalid hexadecimal length ");
exit(0);
}
//Print out of polynomial value in binary
printf(" The polynomial that was used (binary bit string): %s ", poly);
fflush(stdout);
//Fifth output if calculation is selected
calcPrint(argv[1]);
return 0;
}
//Function slects mode type for first argument
void modeType(char *argv) {
if(*argv == 'v') {
printf("verification ");
fflush(stdout);
}
else if(*argv == 'c') {
printf("calculation ");
fflush(stdout);
}
else {
printf("ERROR - Invalid argument ");
fflush(stdout);
exit(0);
}
}
//Function converts hex to binary values
void hexConvert(char hex[60]) {
int i = 0;
while (i < strlen(hex)) {
if (hex[i] == '0')
printf("0000 ");
else if (hex[i] == '1')
printf("0001 ");
else if (hex[i] == '2')
printf("0010 ");
else if (hex[i] == '3')
printf("0011 ");
else if (hex[i] == '4')
printf("0100 ");
else if (hex[i] == '5')
printf("0101 ");
else if (hex[i] == '6')
printf("0110 ");
else if (hex[i] == '7')
printf("0111 ");
else if (hex[i] == '8')
printf("1000 ");
else if (hex[i] == '9')
printf("1001 ");
else if (hex[i] == 'A')
printf("1010 ");
else if (hex[i] == 'B')
printf("1011 ");
else if (hex[i] == 'C')
printf("1100 ");
else if (hex[i] == 'D')
printf("1101 ");
else if (hex[i] == 'E')
printf("1110 ");
else if (hex[i] == 'F')
printf("1111 ");
i++;
}
}
//Function prints out append output in "calculation" mode
void calcPrint(char *argv) {
if (*argv == 'c') {
printf("Number of zeroes that will be appended to the binary input: 12 ");
fflush(stdout);
}
}
Explanation / Answer
//Libraries
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Functional Prototypes
void modeType(char *argv);
void hexConvert(char hex[60]);
void hexConvert_from_char(char hex); // added one more function to convert a
// hex char to binary
void calcPrint(char *argv);
//Polynomial: x^12 + x^11 + x^7 + x^5 + x^4 + x^1 + 1
const char poly[17] = "1100 0101 1001 1";
void process_last_3_digits(char * str){
// function to print last line
int len = strlen(str);
printf("The CRC observed at the end of the input :");
// convert last 3 chars
hexConvert_from_char(str[len - 3]);
hexConvert_from_char(str[len - 2]);
hexConvert_from_char(str[len - 1]);
printf(" (bin) = ");
printf("%c%c%c (hex) ", str[len-3], str[len - 2], str[len - 1] );
}
int main (int argc, char *argv[]) {
//Header print out
printf("CRC Tester by [Your-name] ");
fflush(stdout);
//Error and exit out if argv are not entered correctly
if(argc != 3) {
printf("ERROR - Entered invlaid number of arguments ");
fflush(stdout);
exit(0);
}
//Mode type print out
printf("Mode of operation: ");
fflush(stdout);
modeType(argv[1]);
//Hex value print out
printf("The input string (hex): %s ", argv[2]);
fflush(stdout);
//Binary print out of hex values
if (strlen(argv[2]) >= 3 && strlen(argv[2]) < 41) {
printf("The input string (bin): ");
fflush(stdout);
hexConvert(argv[2]);
printf(" ");
fflush(stdout);
}
else {
printf("ERROR - Invalid hexadecimal length ");
exit(0);
}
//Print out of polynomial value in binary
printf(" The polynomial that was used (binary bit string): %s ", poly);
fflush(stdout);
//Fifth output if calculation is selected
//calcPrint(argv[1]);
process_last_3_digits(argv[2]);
return 0;
}
//Function slects mode type for first argument
void modeType(char *argv) {
if(*argv == 'v') {
printf("verification ");
fflush(stdout);
}
else if(*argv == 'c') {
printf("calculation ");
fflush(stdout);
}
else {
printf("ERROR - Invalid argument ");
fflush(stdout);
exit(0);
}
}
//Function converts hex to binary values
void hexConvert(char hex[60]) {
int i = 0;
while (i < strlen(hex)) {
if (hex[i] == '0')
printf("0000 ");
else if (hex[i] == '1')
printf("0001 ");
else if (hex[i] == '2')
printf("0010 ");
else if (hex[i] == '3')
printf("0011 ");
else if (hex[i] == '4')
printf("0100 ");
else if (hex[i] == '5')
printf("0101 ");
else if (hex[i] == '6')
printf("0110 ");
else if (hex[i] == '7')
printf("0111 ");
else if (hex[i] == '8')
printf("1000 ");
else if (hex[i] == '9')
printf("1001 ");
else if (hex[i] == 'A')
printf("1010 ");
else if (hex[i] == 'B')
printf("1011 ");
else if (hex[i] == 'C')
printf("1100 ");
else if (hex[i] == 'D')
printf("1101 ");
else if (hex[i] == 'E')
printf("1110 ");
else if (hex[i] == 'F')
printf("1111 ");
i++;
}
}
//Function converts hex char to binary values, same as above but only for a char
void hexConvert_from_char(char hex) {
if (hex == '0')
printf("0000 ");
else if (hex == '1')
printf("0001 ");
else if (hex == '2')
printf("0010 ");
else if (hex == '3')
printf("0011 ");
else if (hex == '4')
printf("0100 ");
else if (hex == '5')
printf("0101 ");
else if (hex == '6')
printf("0110 ");
else if (hex == '7')
printf("0111 ");
else if (hex == '8')
printf("1000 ");
else if (hex == '9')
printf("1001 ");
else if (hex == 'A')
printf("1010 ");
else if (hex == 'B')
printf("1011 ");
else if (hex == 'C')
printf("1100 ");
else if (hex == 'D')
printf("1101 ");
else if (hex == 'E')
printf("1110 ");
else if (hex == 'F')
printf("1111 ");
}
//Function prints out append output in "calculation" mode
void calcPrint(char *argv) {
if (*argv == 'c') {
printf("Number of zeroes that will be appended to the binary input: 12 ");
fflush(stdout);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.