Skip to main content

Discrete Mathematics for Computing (Draft)

Section 3.5 Building a Base-Conversion Function

Note. This section asks you to construct a base-conversion function using computer code. Each task includes a prompt to write pseudocode to construct the corresponding function. The first two such prompts are accompanied by interactive exercises asking you to organize provided pseudocode into an order which will achieve the desired result. Subsequent tasks ask you to write your own pseudocode from scratch. Accompanying each task are also interactive code blocks which invite you to write your own working function in a language of your choice. The existing interactive code blocks allow Sage, Python, and R.
In order to successfully navigate this section, you’ll need to recall how base-conversion works (from Chapter 1). You’ll also need to use what we learned about logic and how it can be used for programmatic flow control. That is, you’ll need to recall what we learned about If/Then statements. You’ll also need to make use of for and while loops. This section is a great opportunity to connect our discrete math content to the topics you’ve been learning and implementing in your introductory coding course.
Let’s start with a function that converts decimal numbers into their corresponding binary representations.
  • Start with a pseudocode outline for your function. Pseudocode is a very high-level description of the steps a program must complete, beginning with 0 or more inputs, before returning a desired output. Write a pseudocode description of this function.

Checkpoint 3.5.1. Write Pseudocode 1.

Drag and drop the blocks of pseudocode below to outline a program which will convert a decimal integer into its binary equivalent.
  • Choose your favorite programming language and try to transform your pseudocode into a working function.
  • Whether or not you’ve chosen to build a working version of the function to take a decimal integer and convert it into binary, think about whether your code/pseudocode will handle special ("edge") cases. Is your function capable of converting 0? Can it handle negative integers as inputs? How does your function behave when you pass it a non-integer value as an input? Try updating your code/pseudocode to handle these things.
Build a function which will convert a bit-string (binary string) to its corresponding decimal representation.
  • Start with a pseudocode outline for the function.

Checkpoint 3.5.2. Write Pseudocode 2.

Drag and drop the blocks of pseudocode below to outline a program which will convert an integer from its binary representation to its decimal representation.
  • Try taking your pseudocode and implementing it as a working function in your language of choice.
  • Does your function "work" if you accidentally provide it a non-binary input? We better update the function to check this. We don’t want users to get an output if they don’t provide a valid input. Make the corresponding update to your function.
  • Does your error check use a separate loop to ensure that the user input is binary? If so, can you make your function more efficient by using just a single loop? NOTE: We’ll return to the idea of looping and efficiency later in our course.
Now that you’ve made it this far, maybe we don’t want to restrict ourselves to working only in binary. Make updates to your previous two functions so that they can convert to and from decimal representations and any other base less than 10. HINT: You’ll need to allow for users to input both the value to be converted as well as the base you are working with in each function.
  • Create a new version of your first function which will convert a decimal integer to base-\(b\text{,}\) where \(2\leq b \lt 10\text{.}\)
  • Create a new version of your second function which will convert an integer written in base-\(b\text{,}\) where \(2\leq b \lt 10\text{,}\) into its decimal representation.
  • Check to ensure your functions handle "edge" cases, like converting 0, dealing with negative integers, and checking to ensure valid inputs before returning a converted value.
You now have all of the tools you need in order to piece together a function that will convert between two bases \(b_1\) and \(b_2\text{,}\) with \(2\leq b_1, b_2 \leq 10\text{.}\)
  • Build a new function, calling the functions you’ve written so far as helpers, to convert between any two bases (as long as those bases are at most base-10). Your function will need three input values: initial_value, initial_base, and target_base.
As one more challenge, try building a function which will convert a decimal integer value into its hexadecimal representation. You’ll get more practice with if / else if, and else statements here.