CS520
Fall 2013
Lab 1
Due Wednesday, August 28
Write a program that takes a single command line argument and outputs
the unsigned integer from the command line argument in little endian form in
hex.
Example:
-
./hex_int 0
Should output
00 00 00 00
-
./hex_int 1
Should output
01 00 00 00
Detailed instructions
- Log on to one of the computers in the lab.
- Open up a terminal
- Make a directory entitled lab1
mkdir lab1
- Change to the lab1 directory
cd lab1
- In the lab1 folder, create a file called hex_int.c and write your
program in this file.
- To compile your file, in the lab1 directory type:
gcc -g -Wall -o hex_int hex_int.c
- -g indicates to compile the program with debugging information
- -o hex_int makes the compiler name the resulting binary hex_int
instead of a.out
- When you are done, submit your source file.
~cs520/bin/DoSubmission.py lab1 hex_int.c
Submitting
Please submit whatever you have at the end of lab, even if it is
incomplete. You have the rest of today to finish this assignment.
Grading
- 75 points will be awarded for correctly outputting ordinary
numbers.
- 5 additional points will be awarded for making sure there are the
correct number of command line arguments.
- 10 additional points will be awarded for detecting invalid input
cmo66@zelinka:~/cs520/p1/lab1$ ./hex_int chris
chris is not a valid number
- 10 additional points will be awarded for detecting overflow
cmo66@zelinka:~/cs520/p1/lab1$ ./hex_int 9999999999999999999999999999999
9999999999999999999999999999999 is not a valid number (overflow)
Stuff you may find helpful
Printing an unsigned character
int x;
printf("%d ", x);
This is how you just write out an integer in decimal followed by a space. If you want to
write out a character, you can basically do the same thing.
unsigned char x;
printf("%d ", x);
What if you want it printed nicely in hex?
unsigned char x;
printf("%x ", x);
What if you want the number to always take up 2 spaces?
unsigned char x;
printf("%02x ", x);
How do you turn a string into an integer?
There are a number of choices, I recommend strtoul.
This function will allow you to detect all of the different error
situations necessary for full credit.
Next Steps
The basics of this program are fairly straightforward, so it would not
surprise me if some students finished early.
Writing to a file
I would recommend figuring out how to write numbers in little endian
out to a file. Although there are a number of ways to write to a
file, I found the function putc(int
character, FILE* stream) to be useful for doing this.
Once you have attempted to write something to a file, you should check
on what you wrote to make sure you like what it looks like. Usually
you open up something you wrote in a text editor and make sure it is
correct, but that won't work here, because if you did your job right,
you won't have any text to look at.
The easiest way to look at something in hex is to use od:
cmw@paris:~/cs520/p1/converter$ od -x output
0000000 d950 df21 3412
0000006
Reading from a file
To do program 1, you will also need to read something in binary from a
text file.