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:

Detailed instructions

  1. Log on to one of the computers in the lab.
  2. Open up a terminal
  3. Make a directory entitled lab1
    mkdir lab1
  4. Change to the lab1 directory
    cd lab1
  5. In the lab1 folder, create a file called hex_int.c and write your program in this file.
  6. To compile your file, in the lab1 directory type:
    gcc -g -Wall -o hex_int hex_int.c
  7. When you are done, submit your source file.
    ~cs520/bin/DoSubmission.py lab1 hex_int.c


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.


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

Reading from a file

To do program 1, you will also need to read something in binary from a text file.