Help... conversion of hex to dec
Author Message
Help... conversion of hex to dec

What's the simplest way of converting a hex value to a dec value?

e.g :

00 - FF should become 0.0 to 5.0.

Anyone able to help me?

Mon, 02 Sep 2002 03:00:00 GMT
Help... conversion of hex to dec
Basil Brush a crit dans le message ...

Quote:
>What's the simplest way of converting a hex value to a dec value?

>e.g :

>00 - FF should become 0.0 to 5.0.

In what base ?

--
-hs- "Stove"
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC

Mon, 02 Sep 2002 03:00:00 GMT
Help... conversion of hex to dec

Quote:

>What's the simplest way of converting a hex value to a dec value?

>e.g :

>00 - FF should become 0.0 to 5.0.

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.

-- Mat.

Mon, 02 Sep 2002 03:00:00 GMT
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.

Tue, 03 Sep 2002 03:00:00 GMT
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:

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.

+2500  // plus a half-way adjustment,
+128; // plus a prerounding adjustment.
lADVal /= 256;      // divide by the total number of readings possible.

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

Tue, 03 Sep 2002 03:00:00 GMT
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??

If it is linear, then the following works.  Most A/D converters include
a scaling factor or some other non-linearity, however.

#include <stdio.h>

int main(void)
{
unsigned x;
for (x = 0; x < 0x100; x++)
printf("0x%02x / 51. = %f\n", x, x / 51.);
return 0;

Quote:
}

--

What one knows is, in youth, of little moment; they know enough who
know how to learn. - Henry Adams

__________________________________________________________
Fight spam now!
Get your free anti-spam service: http://www.brightmail.com

Tue, 03 Sep 2002 03:00:00 GMT
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??

Erm. This is an utterly different question to your original one.

You want to return a double, which represents the equivalent to an
unsigned char, such that
(uchar)0 ->(double)0
and
(uchar)0xff ->(double) 5.0

double GetVolts(unsigned char c)
{
v = something_to_do_with_c_that_turns_255_into_5.0
return v;

Quote:
}

I suspect that this involves not a lot more than the * and / operators

Mark McIntyre

C- FAQ: http://www.eskimo.com/~scs/C-faq/top.html

Tue, 03 Sep 2002 03:00:00 GMT
Help... conversion of hex to dec

Quote:
> Erm. This is an utterly different question to your original one.

I just got a little confused. Sorry.

Cheers to all for the help....

Wed, 04 Sep 2002 03:00:00 GMT

 Page 1 of 1 [ 8 post ]

Relevant Pages