/////////////////////////////////// // // program_8 project // // created 10/5/21 by tj // rev 0 // /////////////////////////////////// // // Converting unsigned 8 bit to binary values for printing // // using an array to store the bits // // input - number from user // output - binary value // /////////////////////////////////// #include "mbed.h" #include // only needed when printing // function prototypes uint8_t get_value(void); void print_binary_8(uint8_t dec); int main(void){ setbuf(stdout, NULL); // disable buffering when printing // splash printf("\n\nprogram_8\n"); printf("Using Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); printf("Welcome to my binary printing program\n\n"); uint8_t input_val; while(1){ input_val = get_value(); print_binary_8(input_val); }// end while return 0; }// end main uint8_t get_value(void){ int tmp_val; printf("Please enter a decimal value to convert to unsigned 8 bit binary: "); scanf("%i", &tmp_val); while ((tmp_val < 0)|| (tmp_val > 255)){ printf("%i cannot be represented by an 8 bit unsigned binary number!\n\n", tmp_val); printf("Please enter a decimal value to convert to insigned 8 bit binary: "); scanf("%i", &tmp_val); }// end while return (uint8_t)tmp_val; }// end get_value void print_binary_8(uint8_t dec){ int i; int tmp_dec; uint8_t bin[8]; tmp_dec = dec; for(i=0; i < 8; i++){ bin[i] = tmp_dec % 2; tmp_dec = tmp_dec / 2; } printf("The value %i is ", dec); for(i=7; i >= 0; i--) printf("%i", bin[i]); printf(" in 8 bit unsigned binary\n\n"); return; }// end print_binary_8