Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function void btoh(unsigned char byte, char digit1, char digit0) that co

ID: 3628252 • Letter: W

Question

Write a function void btoh(unsigned char byte, char digit1, char digit0) that converts an unsigned char argument into 2 hexadecimal digits and prints the hex digits as ASCII characters to standard output. Also write a test program btoh_test.c to read characters from standard input and write output to standard output as hexadecimal.

Explanation / Answer

/* *btoh_text.c */ static const char *hexDigits = "0123456789ABCDEF"; void btoh(unsigned char byte, char *digit1, char *digit0) { char lowerNibble = byte & 0x0F; *digit0 = hexDigits[(int) lowerNibble]; char upperNibble = byte >> 4; *digit1 = hexDigits[(int) upperNibble]; } /* * btoh_test.c */ #include #include int main(int argc, char **argv) { int bytes_read; int nbytes = 10; char *my_string; my_string = (char *) malloc(nbytes + 1); char c, c1, c2; while (1) { printf("Input a single character: "); fflush(stdout); bytes_read = getline(&my_string, &nbytes, stdin); if (bytes_read < 2) { break; } c = my_string[0]; if( (c == ' ') || (c == ' ')) { break; } printf("Character in is '%c' ", c); btoh(c, &c1, &c2); printf("Hexadecimal output is %c%c ", c1, c2); } return 0; }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote