f



hexadecimal to decimal conversion

i wanu write a code without using <string.h> by using <stdio.h> and <math.h>. But i m not understanding how to declare inputs from A to F in hex. I mean how to write them in my code and then in output it displays the corresponding decimal value. 
0
sadafbasit73
12/24/2016 3:37:31 PM
comp.lang.c 30656 articles. 5 followers. spinoza1111 (3246) is leader. Post Follow

4 Replies
715 Views

Similar Articles

[PageSpeed] 51

So yeah. Last night my girlfriend let out this really loud fart right after we had sex. It was pretty unromantic.
0
Chad
12/24/2016 4:08:18 PM
sadafbasit73@gmail.com wrote:
> i wanu write a code without using <string.h> by using <stdio.h> and
> <math.h>. But i m not understanding how to declare inputs from A to F in
> hex. I mean how to write them in my code and then in output it displays the
> corresponding decimal value.

I don't know what you mean with "inputs from A to F". Since
you want to use nothing but what's from <stdio.h> I guess
you want the input to come from the terminal or a file.
And then all you get are simple characters, nothing to do
with hex numbers per se - that's an interpretation of the
input (e.g. "bed", "dead" or "beef" could be seen as words
or as hexadecimal numbers).

Here's a trivial program for converting input, interpreted
as a hex number, to a decimal number. You don't even need
anything from <math.h> for that.

#include <stdio.h>

int
main(void)
{
    unsigned int res = 0;

    while (1) {
        int c = getchar();

        if (c >= '0' && c <= '9')
            res = 16 * res + c - '0';
        else if (c == 'a' || c == 'A')
            res = 16 * res + 10;
        else if (c == 'b' || c == 'B')
            res = 16 * res + 11;
        else if (c == 'c' || c == 'C')
            res = 16 * res + 12;
        else if (c == 'd' || c == 'D')
            res = 16 * res + 13;
        else if (c == 'e' || c == 'E')
            res = 16 * res + 14;
        else if (c == 'f' || c == 'F')
            res = 16 * res + 15;
        else
            break;
    }

    printf("%u\n", res);
    return 0;
}

If course, there's an obvious flaw. What's to be done if
d the input represents too large a number to be stored in
an unsigned int? That's something you might want to add.

                      Regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt@toerring.de
   \__________________________      http://toerring.de
0
jt
12/24/2016 4:18:03 PM
Chad wrote:
> So yeah. Last night my girlfriend let out this really loud fart
> right after we had sex. It was pretty unromantic.

Chad, there is another way to be.  Right now you are vulgar, crass, hateful,
and mean.  You are also engaging in fornication, a sin before God.

God created us to love one another, and help one another, and to
serve Him in this fallen world, to teach other people those things
He first taught us through His Son, Jesus Christ.

There is also an enemy of God at work in this world.  Today, you are
serving that enemy ... which also makes you an enemy of God, and
destroys your eternal soul.

The way out of that state requires action on your behalf, because
God will honor your choice:  rightness, or wrongness; holiness,
or sin; asking forgiveness from Jesus Christ, or rebelling against Him.
But because He is God, you should listen to His warning about the end
of all who follow after the enemy, for it is eternal death in Hell.

But forgiveness is free to receive, and it follows truth in all things,
and is the right way to be.

It's your choice.  Consider your life now, and in eternity, and choose wisely.

Best regards,
Rick C. Hodgin
0
Rick
12/24/2016 4:24:36 PM
On Saturday December 24 2016 10:37, in comp.lang.c, "sadafbasit73@gmail.com"
<sadafbasit73@gmail.com> wrote:

> i wanu write a code without using <string.h> by using <stdio.h> and
> <math.h>. But i m not understanding how to declare inputs from A to F in
> hex. I mean how to write them in my code and then in output it displays the
> corresponding decimal value.

So, let's see....

The character
  '0' will have a corresponding hexadecimal value of 0x00,
  '1' will have a corresponding hexadecimal value of 0x01,
  '2' will have a corresponding hexadecimal value of 0x02,
  '3' will have a corresponding hexadecimal value of 0x03,
  '4' will have a corresponding hexadecimal value of 0x04,
  '5' will have a corresponding hexadecimal value of 0x05,
  '6' will have a corresponding hexadecimal value of 0x06,
  '7' will have a corresponding hexadecimal value of 0x07,
  '8' will have a corresponding hexadecimal value of 0x08,
  '9' will have a corresponding hexadecimal value of 0x09,
right?

And, the character
  'A' will have a corresponding hexadecimal value of 0x0A,
  'B' will have a corresponding hexadecimal value of 0x0B,
  'C' will have a corresponding hexadecimal value of 0x0C,
  'D' will have a corresponding hexadecimal value of 0x0D,
  'E' will have a corresponding hexadecimal value of 0x0E,
  'F' will have a corresponding hexadecimal value of 0x0F,
right?

And, you want to loop through your input, testing each string character if it
is one of those characters, right?
And, /if/ the string character /is/ one, you want to use the corresponding
hexadecimal value, right?

Does this suggest something to you?



-- 
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request

0
Lew
12/24/2016 4:25:44 PM
Reply: