# Assignment Zero: Base Conversion

You are to write a simple C program that will take a decimal number and a base as command line arguments and output the decimal number in that specified base. For example:

```
$ ./conv 13 2
1101
$ ./conv 236709 8
716245
$ ./conv 1352275 11
843A91
$ ./conv 38572856 64
2J9Cu
$ ./conv 3232236286 256
192.168.2.254
$ ./conv
Usage: conv <decimal> <base>
$ ./conv 11111 65
INVALID BASE
```

- Your program should be able to handle all bases from 2 to 64 and 256.
- For bases that are powers of 2 (2, 4, 8, 16, 32, 64 and 256) you should use a
**mask and shift**algorithm. See below. - Please name your code file
`conv.c`

. This will be the only file you submit to me (via the git server, we will get you connected to this on Thursday,*so come prepared*). - Attempt to handle errors. Another problem could be an invalid base (see example output).
- Use a buffer of
**32 characters**. This will give you 31 locations for characters and the final location should be a null. - Remember: you have to put the digits into the buffer
*backwards*and when the algorithm is finished, you have to*printf from the correct location in that buffer*. See the example we did in class for how to accomplish that.

### Converting Integers to Characters

Think about how to use this character array:

`char *ascii = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";`

You can declare this at the top of your file along with your buffer. How can you use this to accomplish a conversion to bases 11 to 64? Think about how to do this! It is just an array of characters ...

### Masking & Shifting

Along with the algorithm we saw in class of modulo followed by division, you can accomplish the same result on bases that are powers of two *by masking off the number of bits you need and then shifting the bits down*. Remember how the `&`

operator works? What if you are trying to convert the number 7 into base 4? How many bit patterns are there for base 4: `00`

, `01`

, `10`

and `11`

which is 0, 1, 2 and 3. Let's see how masking works:

```
0111 = 7
& 0011 = 3
__________
11 = 3
```

So, you've "masked off" the first digit by using the `&`

(bitwise and) operator instead of modulo. Now you can shift those bits to the right instead of using the division operator like so:

```
0111 >> 2 = 01
```

And continue with the algorithm:

```
01 = 1
& 11 = 3
________
01 = 1
```

So the number 7 in base 4 is: 13!

*You must use this algorithm for the specified bases!*