Schedule‎ > ‎

Lab 11: Decimal to Binary


  • Explore how to convert decimal numbers to binary.

^ top

Academic Honesty

Read the Scholastic Honesty Policy and Assignment Integrity policies of the syllabus. Here are some clarifications for this particular assignment:

  • You must breadboard the circuits yourself, though you may get help from others if you get stuck.
  • You may discuss the code with other students for this lab.
  • You must enter all the code yourself and not copy from another student.

^ to

Part 1: Decimal to Binary Conversion

In this lab you write a program to convert decimal numbers to binary numbers.

Starter Code

void setup() {
cout << "Enter a number: " << endl;

void loop() {
if (Serial.available()) {
int x = 0;
cin >> x;
String binStr = toBinaryStr(x);
cout << "binary for decimal " << x << " is ";
Serial.print(binStr); //must use Serial.print for a String
cout << "\nEnter a number: ";

String toBinaryStr(long num) {
return "";


  1. Start with a new sketch and copy the Starter Code into the sketch.
  2. Save the sketch using the name dec2bin.
  3. Notice that x is declared as an int,  but toBinaryStr() takes a long parameter. 
  4. Use a cast operator to temporarily cast x as a long when you call toBinaryStr().  See HERE for a refresher on type conversion.
  5. In the toBinaryStr() function, add the following code before the return statement.
    if (num == 0) {
      return "0";
  6. After the if-statement, add the following code:
      String binary = "";
    while (num != 0) {
    cout << "num = " << num << " num % 2 is " << num % 2 << endl;
    binary = num % 2 + binary; /*next binary number is num % 2,
    concatenate it to left side of current binary string*/
    num = num / 2; //shift num to right

    Notice that binary numbers are the remainder after division by two. We simply concatenate the binary numbers to the front of the string.

  7. Revise the current return statement to return the binary variable instead of "".
  8. In your HEADER comment, show the operation of your program for the numbers 8, 0, -8 and at least two original examples.
  9. Submit your dec2bin.ino file as part of this lab.

Answer these questions in the comment area of Canvas:

Be prepared to answer the following questions after completing this lab:

  1. The reason for the if-statement in the toBinaryStr() function is the ________.
    1. body of the while loop does not execute when num == 0
    2. program needs to return an empty string ("")
    3. string "0" is known as an empty string
    4. return statement will not return an empty string ("") so we need to add a character
  2. True or false: binary numbers can be computed from repeated integer division by two with the remainder becoming the binary digits.
  3. True or false: concatenation means that we join two strings into one string.
  4. The binary number for 42 is: ________.
    1. 010101
    2. 101010
    3. 101011
    4. 1010101
       5.   What is the purpose of doing the typecast of integer x as specified in Step 4.

^ top

Grading Criteria (10 pts)

Note that function headers must have the @param and @return comments filled in.

Each Part:  10 points  (Header 1pt, Function Headers 1pt, Compiles 1 pts, Proper Formatting 1 pt,  Works as Specified 4 pts,  Questions 2pts)

Part 1:  10 points possible

^ top