University of California at Berkeley
College of Engineering
Department of Electrical Engineering and Computer Science

CS61CL, Summer 2009

HW 3 (Updated 07/02)

Changes to the spec will be posted in red.

[TA] James Tu

Due Monday, July 6, 2009

Submitting your Solutions

Submit your solution by creating a directory named hw3 that contains files named htoi.c and baseconvert.c. From within that directory, type "submit hw3". This is not a partnership assignment; hand in your own work.

Problem 1

Complete Exercise 2-3 on page 46 of K&R which is as follows:

Write a function htoi(s), which converts a string of hexadecimal digits (including an optional 0x or 0X) into its equivalent integer value. The allowable digits are 0 through 9, a through f, and A through F. Add your function to the following program, which is available online in ~cs61c/code/htoi.c and fill in the identification information.

  
/*  
    Name:
    Lab section time:
    TA: 
*/

#include <stdio.h>


int htoi (char *s)
{
  /* your code here */
}

int main ( )
{
  /* your test hex values -> int here */
}

You may assume that all input is correct (i.e. either 0x or 0X is present or not, followed by 7 hex digits) and may wish to familiarize yourself with bit operations/bit masking for this problem to help you in future MIPS assignments. You may NOT use library functions that do this for you; strtol and strtoul come to mind.

Problem 2

Write a C program called baseconvert.c that takes two command line arguments: number and outbase. "Number" is an non-negative number that is expressed as "number@base" (where base is a non-negative number expressed in base 10) or just "number" (in which case number is interpreted in base 10) and "outbase" is the base that we want to convert "number" to. Your program should print the value represented by number, translated into the outbase. Here are some examples:

command output notes
baseconvert 21 2 10101@2 21 base 10 = 10101 base 2
baseconvert 255@8 34 53@34 255 base 8 = 53 base 34
baseconvert cs@33 25 go@25 cs base 33 = go base 25
baseconvert tu@32 17 356@17 tu base 32 = 356 base 17

We have already provided some of the code for you, which is available online in ~cs61c/code/baseconvert.c. To complete this problem, you need to complete two functions:

  • charVal, which takes a char as an input and returns the numerical value of the character. So, for example:
        charVal('0') = 0
        charVal('a') = 10
        charVal('A') = 10
    Note that if the character is a letter, the letter is case-insensitive and so charVal('a') and charVal('A') should both return 10.

  • int2str, which takes a number and a base that we want to convert number to and returns a string in the format "number@base," in which number has been converted into its new base


    Your program should work with any outbase between 2 and 36. You do not need to check the arguments for errors.

    Updated

    You need to link your program with the math library. For the gcc compiler, you must specify the additional parameter -lm. So, to compile, you must do

    -bash-3.00$ gcc baseconvert.c -lm -o baseconvert

    And then to run, just do

    -bash-3.00$ baseconvert 16 2 => 10000@2

    or if you don't want to name your executable baseconvert

    -bash-3.00$ gcc -lm baseconvert.c

    -bash-3.00$ a.out 16 2 => 10000@2