Skip to content

Project01 - C Base Conversion

Deliverables due Tue Sep 2nd by 11:59pm in your Project01 GitHub repo

Get tests repo here: https://github.com/USF-CS315-F25/tests

Get autograder here: https://github.com/phpeterson-usf/autograder

Dev Environment Requirments

You should have your dev environment working as explained in Dev Setup and required in Lab01. You should be able to:

  1. Use ssh without a password to access the BeagleV machines.
  2. Access GitHub using ssh keys for your repos on the BeagleV machines.
  3. Run the autograder on the BeagleV machines.
  4. Show you can use a console-based editor like micro or vim (not nano).
  5. (Optional) Show you can do all of the above on a local RISC-V Ubuntu VM.

Warmup problem - numinfo

For this problem you will implement a C program called numinfo that takes a string as a single command line argument and determines if the string can represent an integer value (int), and binary value (bin) or a hexadecimal value (hex). The numinfo command works like this:

$ ./numinfo 1010
int: true
bin: true
hex: true

$ ./numinfo 1234
int: true
bin: false
hex: true

$ ./numinfo 1AB3
int: false
bin: false
hex: true

$ ./numinfo 1ab3
int: false
bin: false
hex: true

$ ./numinfo
Usage: numinfo <value>

Note that an integer value must consist of only decimal digits (0, 1, 2, ..., 9). A binary value must consist of only the digits 0 or 1, and a hexadecimal value must consist of only digits or the letters (a, b, c, d, e, f, A, B, C, D, E, F). You should implement your solution by writing several helper functions:

bool is_dec_digit(char c);
bool is_bin_digit(char c);
bool is_hex_digit(char c);
bool is_dec_str(char *s);
bool is_bin_str(char *s);
bool is_hex_str(char *s);

Number Base Conversion - numconv

You will implement a base conversion tool called numconv. It converts numbers expressed in bases 2, 10, and 16 into the other bases. The numconv program can optionally output the conversion in multiple bases. The order of the output bases is fixed: base2, base10, then base16. Examples:

$ ./numconv 10 -o 2
0b1010
$ ./numconv 0xFF -o 10
255
$ ./numconv 0b11011110101011011011111011101111 -o 16
0xDEADBEEF
$ ./numconv 0b11111111111111111111111111111111 -o 10
4294967295
$ ./numconv -o 10 0x0000000B
11
$ ./numconv 0b123 -o 2
Bad input
$ ./numconv 213 -o 2 -o 16
0b11010101
0xd5
$ ./numconv 213 -o 2 -o 16 -o 10
0b11010101
213    
0xd5
  1. You must implement the base conversions yourself, without using C library printf("%d"), printf("%x"), scanf(), or atoi()
  2. You need to create a Makefile which builds the executables numinfo and numconv. You need to implement numinfo.c and numconv.c
  3. The basic idea is to covert the input number as a string into a uint32_t type. Then covert the uint32_t version of the value to one or more output strings in the specified bases.
  4. Note the output order when multiple bases are selected should always be: base 2, base 10, then base 16.

Given

We will review processing command line arguments in C

Pseudocode for uint32_t string_to_int(char *str)

init retval to 0
init placeval to 1 (anything to the 0 power is 1)
determine the base of the str by looking at first two chars
    `0b` means base is 2, `0x` means base is 16, otherwise base is 10
loop over str from the highest index down to 0
    calculate the integer corresponding to the character at that index  
    calculate the value of that place by multiplying the integer * placeval
    add the value to the retval
    update to placeval to placeval * base
return the return value

Pseudocode for void int_to_string(uint32_t value, char *str, int base)

init buffer to empty
while value != 0
    quot = value / base
    rem = value % base
    calculate the character value of rem
    append the character value to the buffer
    value = quot
copy buffer into str in reverse order

Code Quality

You should write and sumbit clean and consistent code. The code should be clear and easy to understand, prefer readable but less efficient code. You should avoid rendancy in your code (that is repeated code). Here are some specific items to pay attention to:

  • Consistent naming of functions and variables
    • In C, snake_case (like is_dec_digit()) is usually preferred over CamelCase (like isDecDigit())
  • Consistent use of vertical space (newlines)
  • Consistent use of horizontal space
    • you use if (x > 1) {, then use this spacing everywhere, not if(x>1){.
  • Consistent use of comments
    • This is includes comment characters /* */ or //
    • Also the capitilzation and punctuation of your comments
  • Consistent placement of braces for functions and statements
  • In you repo you should have no "build artificats"
    • This includes .o files and executables

In summary, your code should be visually consistent and pleasing to the eye.

Rubric

  • 100% Points determined by Project01 autograder tests.
  • 10% deduction for Code Quality issues
    • All Code Quality deductions can be earned back