I have spent almost 2 evenings trying to create a C program to convert Hex (0x) into a decimal number. After re-writing it 3 times, I had figured out the logical structure.

A previous little program I wrote returned the total decimal value of a provided number of bits. For that program, I was able to use:

1 << i;

to provide me with “1 to the power of i”. I couldn’t figure out why this formula didn’t work at all with ^16; 1^16 kept returning a value of *32*. I finally gave up and found a different route.

<math.h> gave me a function that returns the value of a powerof equation.

double pow(double x, double y);

You must also include *-lm* flag whilst compiling. After implementing the function, the whole program started returning correct results. Job done. Here’s the code ( if your interested ) Continue reading “C: Power of (^) & hex to dec converter”

### Like this:

Like Loading...