Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

6.27 LAB: Base Converter In the previous labs, Convert to Binary and ASCII table, the algorithms to convert an integer to binary and integer to

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

6.27 LAB: Base Converter In the previous labs, Convert to Binary and ASCII table, the algorithms to convert an integer to binary and integer to any base were presented and the labs involved writing functions integer_to_binary and integer_to_base. The reverse process to convert a string containing a binary string to a number can be performed by the algorithm number = 0 for each character in the binary string: multiply number by 2 add 1 to the number if the character is '1' The algorithm can be enhanced to convert a number string of any base to an integer as follows: number = 0 for each character in the input string: multiply number by the base add to the number the index position of the character within the list of digits used by the base In hexadecimal the list of digits would be: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', This list of digits can be used for binary, octal, decimal, or any base less than 16, provided the found index is less than the base for the digit to be valid. Define a function base_to_Integer: def base_to_integer (input_string, base = 2): That converts the input string to an integer. Return 'Invalid base' if the base is not between 2 and 16 inclusive. If a character is encountered the is not valid for the selected base (must be in the list and index position must be less than the base) return: "Invalid character '{c}' in string {s} for base {base}" where {C} is the invalid character, {s} is the input string, and {base) is the base. Copy the digits list and integer_to_base function from bases.py file from the ASCII table lab and add the function base_to_integer to that module. The digit list must be given prior to all function definitions. Big help: assuming digits is the name of the digits list and c is a character, use the following statement to find the character's index in the digit list: inx = digits.find(c.upper()) This statement will get the index of the character c within the list provided the character is in the list, otherwise, the index will be-1. Write a program "Base Numeric Converter" that will allow input of a string in any base between 2 and 16 inclusive, converter to an integer, and redisplay the value as decimal, binary, octal, and hexadecimal. The program is to first display help information on using the program and then prompt for input. In addition to an input string to be converted, 'dec', 'bin', 'oct', and 'hex' can be used to select the base accordingly. Furthermore, 'h' can be used to redisplay the help information and 'q' can be used to exit. The prompt will include the selected base. Sample execution: Base Numeric Converter 1.0 Base codes: dec - Decimal bin - Binary oct - Octal hex - Hexadecimal Enter a value, base code to switch base, h to redisplay base codes or q to quit. dec> 843 843 1101001011b 01513 0x34B dec> hex hex> 439A 17306 100001110011010b 041632 0x439A hex> bin bin> 01101 13 1101b o15 OxD bin> 34 Invalid character in string 34 fo 2 bin> oct oct> 342 226 11100010b 0342 OxE2 oct> 011 9 1001b ol1 Ox9 oct> h Base codes: dec - Decimal bin Binary oct Octal hex Hexadecimal Enter a value, base code to switch base, h to redisplay the base codes or q to quit. oct> 9 Big help: suggest writing a while True loop. Provide the user input with the prompt at the top of the loop followed by if, elif ..., else statements. In the if and elif test for "dec", "bin", "oct", "hex", "h' and 'q'. For "dec", "bin", "oct", and "hex" set a prompt string and base variable accordingly. For 'h' print the help menu (see below) and for 'q' do a break statement to exit the loop. In the else call base_\to_integer and print the results accordingly. Define the program in script main.py. The program must use the integer_to_base and base_to_integer functions to perfrom all of the conversions. Define in main.py a function print_menu which is to print: Base codes: dec - Decimal bin - Binary oct Octal hex - Hexadecimal Enter a value, base code to switch base, h to redisplay the base codes or q to quit. Call print_menu() before the loop and from within the loop when the user enters 'h'. Big help: The following if statement can be used to test if the number num returned from base_to_integer returns one of the "Invalid" error messages: if (str(num).startswith("Invalid")): Current file: main.py - Load default template... 1 # These import will allow integer_to_base and base_to_integer to be called without requiring "bases." 2 from bases import integer_to_base 3 from bases import base_to_integer 4 5 #define the function to print the menu here 6 def print_menu(): 7 pass 8 9 if name == 'main': 10 print("Base Numeric Converter 1.6") 11 print() 12 print_menu() 13 14 prompt = 'dec>! 15 base = 10 16 17 # Add code inside this loop 18 while True: 19 input_string = input (prompt) 20 if input_string == 'q': 21 break

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke

1st Edition

0130086509, 978-0130086501

More Books

Students also viewed these Databases questions

Question

How many multiples of 4 are there between 10 and 250?

Answered: 1 week ago