Help... conversion of hex to dec

Quote:

> > First you'd better tell us what you mean by "hex value" and "dec value".

Normal

> > C variables simply store values, with no base implied.

> Sorry...

> Right, using an 8051 and it's A2D function, a voltage source is read.

> This has the range 00 to 255 (00 - FF). With 255 representing 5.0V and 00

> representing 0.0V.

> How can I get this to 0.0 to 5.0??

> Cheers. Sorry about not being clear at first.

Actually, a good approximation for this is to double the A/D value (left

shift once) and then just print it like any other decimal, except with an

implied decimal point after the first digit.

Like this:

int iADVal;

iADVal=reada2d(); iADVal<<=1;

printf("%01i.%02i", iADVal / 100, iADVal % 100);

This will print values from 0.00 to 5.10, so it'll be off by just a wee-bit.

0=0.00, 1=0.02, 2=0.04, ... FE=5.08, FF=5.10

If you're willing to dedicate more code to it, then this is the way to

convert it for real.

long lADVal;

lADVal=reada2d();

lADVal=lADVal*5000 // multiple by total number of millivolts,

+2500 // plus a half-way adjustment,

+128; // plus a prerounding adjustment.

lADVal /= 256; // divide by the total number of readings possible.

printf("%01li.%03li", lADVal / 1000, lADVal % 1000);

All that messy float looking stuff, done with integer math.

0=0.010 (Remember, an A/D zero just means it's less than about

5V/256=19.53mV, so we split the difference).

1=0.029, 2=0.049, ..., FE=4.971, FF=4.990

Is this what you were looking for?

-LZ