Question: a.) Write a program that reads a string (character array) from standard input using scanf and sends to standard output the hexadecimal values of each
a.) Write a program that reads a string (character array) from standard input using scanf and sends to standard output the hexadecimal values of each of the input characters, each on its own line. (Hint: printf can display hexadecimal numbers for you if you give it the right specifier!)
Example input/output: Enter a string: gtfj 67 74 66 6A
b.) Write a function that displays a number in a binary representation. This function takes one parameter: an unsigned int which represents the number to convert. This function uses printf to display the binary value. Your function declaration should look like this:
void display_binary(unsigned int num); You may use any method you can devise to do this conversion. Here's one way to think about this problem. Each bit in an unsigned int can either be on (1) or off (0). You can use the '&' bitwise operator to determine if a particular bit is on or off. You can test if a bit is on by using the expression "x & (1 << n)" where x is the number you're converting and n is the bit number you're testing (which for a 4-byte unsigned int will range from 31 down to 0). If the value of this expression is nonzero you display a '1', otherwise you display a '0'.
You should be able to use your function to display all possible values of unsigned int from 0 to 4294967295 (unsigned int is a 32-bit value on many platforms).
You may include leading zeros in the converted number if you wish.
c.) In your main function, you will ask the user for the value to convert and call your display_binary() function which will display the converted number. Be sure to match the scanf format string specifier to the variable you're reading in!
For example, given the input number 3, the output could be something like one of these:
0000000000000000000000000000011 11 00000011 00000000 00000000 00000000 00000011 In your main function, practice using the bitwise operators & ^ | ~ << >> using values of your choice to perform bitwise operations. Use the display_binary() function you already wrote to display the results of each operation, so that it's easy to see what is being done in each bit by using the bitwise operators. Use each operator at least once. Example:
unsigned int var1 = 30; unsigned int var2 = 5; unsigned int result_and = var1 & var2; printf("Result of & operation: "); display_binary(var1); display_binary(var2); display_binary(result_and);
e.) In your display_binary() function, print a space after each octet (set of 8 bits) to make the number easier to read. You might find the modulus operator % useful.
f.) Make your display_binary() function independent of the size of the type you pass in by using sizeof() on the variable passed in to figure out how many bits it holds. Verify that your program still works correctly by replacing the unsigned int parameter with an unsigned short.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
