# Number Base Conversion Programs

The notation of hexadecimal numbers for this set of programs is that
of the C language where the string "0x" precedes a number
to indicate that it is a hexadecimal number.

• tohex -- a command-line program which takes a number as the first argument and converts it into hexadecimal form. The input integer can be either in decimal or hexadecimal form, where 0x is prepended to the number to indicate hexadecimal form.
```   Usage:  tohex integer

integer = [-0x80000000..0x7fffffff] (in hexadecimal form)
[-2147483648..2147483647] (in decimal form)
```

view source: tohex.cpp

example: 414970 will be converted to 0x654fa.

```
```

• todec -- a command-line program which takes a number as the first argument and converts it into decimal form. The input integer has the same requirements as the tohex program above.

view source: todec.cpp
download binary for: [ Windows95 (old) | SunOS (old) | linux (with libc 6) | linux staticly compiled ]

example: 0x654fa will be converted to 414970.

```
```

• tobin -- a command-line program which converts a decimal or hexadecimal number into a 32-digit binary number. The input integer has the same requirements as the tohex and todec programs above. There is no program to convert from binary to other number bases because you should be able to convert from binary notation to hexadecimal notation with only a little memorization.

view source: tohex.cpp

example: 414970 will be converted to 0000 0000 0000 0110 0101 0100 1111 1010

Conversion between 4 binary digits and 1 hexadecimal digit

```   Decimal  Binary  Hexadecimal       Decimal  Binary  Hexadecimal
0      0000       0                8      1000      8
1      0001       1                9      1001      9
2      0010       2               10      1010      A
3      0011       3               11      1011      B
4      0100       4               12      1100      C
5      0101       5               13      1101      D
6      0110       6               14      1110      E
7      0111       7               15      1111      F
```
So by inspection, the binary number 0010 1100 0110 1011 is equivalent to the hexadecimal number 0x2C6B which according to the todec program is the decimal number 15467.
```

```

craig@ccrma.stanford.edu