# Conversion from Hexadecimal number into Decimal number

Discussion in 'C Programming' started by dharmdeep@gmail.com, Nov 13, 2006.

1. ### Guest

Hi friends,
I need a sample code in C which will convert a Hexadecimal
number into decimal number. I had written a code for that but it was
too long, I need a small code, so request u all to provide me with
small sample code for that.

, Nov 13, 2006

2. ### Ian CollinsGuest

wrote:
> Hi friends,
> I need a sample code in C which will convert a Hexadecimal
> number into decimal number. I had written a code for that but it was
> too long, I need a small code, so request u all to provide me with
> small sample code for that.
>

u is still on holiday.

You show us yours and someone might show you theirs....

--
Ian Collins.

Ian Collins, Nov 13, 2006

3. ### Richard HeathfieldGuest

said:

> Hi friends,
> I need a sample code in C which will convert a Hexadecimal
> number into decimal number.

You ask the impossible. There is no such thing as a hexadecimal number, and
no such thing as a decimal number. Hexadecimal and decimal are merely
representations. The number itself is independent of "base". A dozen apples
is a dozen apples, no matter how many fingers you have, and no matter
whether you write it as 1100, 110, 30, 22, 20, 15, 14, 13, 12, 11, 10, or
C. All that matters is that writer and reader(s) both understand the
representation being used.

What you appear to be asking for is how to convert a given representation
into another representation. That's easy enough to do. Simply build an
integer n from whichever representation you have, and then do this to build
a representation in a given number base:

s = empty string
while n > 0 do
digit = n mod base
add text representation of digit to the end of s
divide n by base, giving n
elihw
if s is empty
s = "0"
else
reverse s in place
fi

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not

Richard Heathfield, Nov 13, 2006
4. ### santoshGuest

Richard Heathfield wrote:
> said:
>
> > Hi friends,
> > I need a sample code in C which will convert a Hexadecimal
> > number into decimal number.

>
> You ask the impossible. There is no such thing as a hexadecimal number, and
> no such thing as a decimal number. Hexadecimal and decimal are merely
> representations. The number itself is independent of "base". A dozen apples
> is a dozen apples, no matter how many fingers you have, and no matter
> whether you write it as 1100, 110, 30, 22, 20, 15, 14, 13, 12, 11, 10, or
> C. All that matters is that writer and reader(s) both understand the
> representation being used.

I would argue that 'dozen' itself is another representation, albiet a
very arbitrary one. I've not studied mathematics beyond junior college,
but let me ask this: Can there be numbers apart from some form of
representation?

santosh, Nov 13, 2006
5. ### Guest

Hello,

> I would argue that 'dozen' itself is another representation, albiet a
> very arbitrary one. I've not studied mathematics beyond junior college,
> but let me ask this: Can there be numbers apart from some form of
> representation?

I don't want to step into abstract mathematical argumentations, but the

http://en.wikipedia.org/wiki/Natural_number

Regards,
Loic.

, Nov 13, 2006
6. ### Richard HeathfieldGuest

santosh said:

> Richard Heathfield wrote:

<snip>

>> A dozen
>> apples is a dozen apples, no matter how many fingers you have, and no
>> matter whether you write it as 1100, 110, 30, 22, 20, 15, 14, 13, 12, 11,
>> 10, or C. All that matters is that writer and reader(s) both understand
>> the representation being used.

>
> I would argue that 'dozen' itself is another representation, albiet a
> very arbitrary one.

Indeed it is. It's "English representation", if you like. Discussing numbers
without representing them in *some* fashion is rather awkward, but consider
the following:

* * * * *
+ + + + +
<>< <>< <>< <>< <><

No matter how you represent the number of asterisks, the number of plus
signs, and the number of fish I just showed, it is nevertheless clear that
there is the same number of each. That sameness, that one-to-one
correspondence, that "matching", is what 'number' is all about.

> I've not studied mathematics beyond junior college,
> but let me ask this: Can there be numbers apart from some form of
> representation?

Very much so, yes - but talking about them can get a bit abstract at times.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not

Richard Heathfield, Nov 13, 2006
7. ### SM RyanGuest

wrote:
# Hi friends,
# I need a sample code in C which will convert a Hexadecimal
# number into decimal number. I had written a code for that but it was
# too long, I need a small code, so request u all to provide me with
# small sample code for that.

strtol(string,0,16)

--
SM Ryan http://www.rawbw.com/~wyrmwif/
No pleasure, no rapture, no exquisite sin greater than central air.

SM Ryan, Nov 13, 2006
8. ### Frederick GothamGuest

:

> Hi friends,
> I need a sample code in C which will convert a Hexadecimal
> number into decimal number. I had written a code for that but it was
> too long, I need a small code, so request u all to provide me with
> small sample code for that.

Your question is very vague and generic, so I'll give you code which is
rather generic.

A number will be represented by a sequence of digits. What's a digit? Is it
a character? Is it a floating-point number? Is it a pointer? I don't know,
so I'll write generic code.

How will we arrange these digits? Well let's decide that we'll have a null-
terminated array which starts with the Most Significant Digit.

In providing a function which converts from one representation to another,
we need also to receive a look-up table to map a digit value to an actual
digit (and vice-versa). This look-up may come in the form of a function, or
of an array. I think a function might be more convenient. Now, we can write
the function as follows:

(Unchecked, may contain a subtle bug or two.)

#include <assert.h>

unsigned (*const pFrom)(DigitType),
DigitType (*const pTo)(unsigned))
{

{
long unsigned val = 0;
long unsigned multiplier = 1;

DigitType *p = p_arr;

val += pFrom(*p++);

while (*p) val += pFrom(*p++) * (multiplier*=from_radix);

p = p_arr;
do
{
} while (val);

*p = 0;
}}

As you can see, the function assumes the precondition that the buffer is
long enough. It may be used something like the following:

(Again, unchecked.)

#include <assert.h>

typedef char DigitType;

#include "cbr.h" /* Code shown above */

unsigned DecToVal(DigitType const x)
{
assert(x>='0' && x<='9');

return x - '0';
}

DigitType ValToHex(unsigned const x)
{
assert(x<=15);

if (x < 10) return '0' + x;

switch(x)
{
case 10: return 'A';
case 11: return 'B';
case 12: return 'C';
case 13: return 'D';
case 14: return 'E';
case 15: return 'F';
}
}

#include <stdio.h>

int main(void)
{
char buf[24] = "654323";

puts(buf);

return 0;
}

It was only after having written the code that I realised we* write from
left to right putting the Most Significant Digit first, rather than the
Least Significant Digit. Ah well.

*we : except for our Arabic particpants of course!

--

Frederick Gotham

Frederick Gotham, Nov 13, 2006
9. ### Richard BosGuest

SM Ryan <> wrote:

> wrote:
> # I need a sample code in C which will convert a Hexadecimal
> # number into decimal number. I had written a code for that but it was
> # too long, I need a small code, so request u all to provide me with
> # small sample code for that.

# error This is not a quoting character suitable for Usenet.
> strtol(string,0,16)

That's a function that will turn the hexadecimal text representation of
a number into an actual number, in binary. To turn _that_ into a decimal
number, you need another function which is equally easily found in any
handbook on C.

Richard

Richard Bos, Nov 13, 2006
10. ### David T. AshleyGuest

<> wrote in message
news:...
> Hi friends,
> I need a sample code in C which will convert a Hexadecimal
> number into decimal number. I had written a code for that but it was
> too long, I need a small code, so request u all to provide me with
> small sample code for that.

Although they're a bit pricey, I would recommend you buy Knuth's classic
3-volume work "The Art of Computer Programming". Knuth covers all the basic
integer algorithms there, including radix conversion.

The other poster (who included the C-code) appears at first glance to have
simply provided you the standard algorithm. Very nice of him, but a waste
of his time.

Dave.

David T. Ashley, Nov 13, 2006
11. ### RichardGuest

"santosh" <> writes:

> Richard Heathfield wrote:
>> said:
>>
>> > Hi friends,
>> > I need a sample code in C which will convert a Hexadecimal
>> > number into decimal number.

>>
>> You ask the impossible. There is no such thing as a hexadecimal number, and
>> no such thing as a decimal number. Hexadecimal and decimal are merely
>> representations. The number itself is independent of "base". A dozen apples
>> is a dozen apples, no matter how many fingers you have, and no matter
>> whether you write it as 1100, 110, 30, 22, 20, 15, 14, 13, 12, 11, 10, or
>> C. All that matters is that writer and reader(s) both understand the
>> representation being used.

>
> I would argue that 'dozen' itself is another representation, albiet a
> very arbitrary one. I've not studied mathematics beyond junior college,
> but let me ask this: Can there be numbers apart from some form of
> representation?
>

Tell me, how is "dozen" any more arbitrary than "twelve"? It isnt.

--

Richard, Nov 14, 2006
12. ### James Dow AllenGuest

Ian Collins wrote:
> You show us yours and someone might show you theirs....

Uh, isn't there an alt.sex.personals for
queries of this ilk?

James

James Dow Allen, Nov 14, 2006
13. ### santoshGuest

On Nov 13, 1:18 am, wrote:
> Hi friends,
> I need a sample code in C which will convert a Hexadecimal
> number into decimal number. I had written a code for that but it was
> too long, I need a small code, so request u all to provide me with
> small sample code for that.

Just use strtoul() or the like. sscanf() is somewhat easier to use but
is less robust.

santosh, Nov 14, 2006
14. ### Random832Guest

2006-11-14 <>,
santosh wrote:
> On Nov 13, 1:18 am, wrote:
>> Hi friends,
>> I need a sample code in C which will convert a Hexadecimal
>> number into decimal number. I had written a code for that but it was
>> too long, I need a small code, so request u all to provide me with
>> small sample code for that.

>
> Please reply to the group as I don't check email often.
>
> Just use strtoul() or the like. sscanf() is somewhat easier to use but
> is less robust.

sscanf() is only easier to use if you use it wrong.

I would bet, though, that he's got a homework assignment to do it
himself.

To the original poster: post your existing code and maybe people can
give you ideas on how to tighten it up.

Random832, Nov 14, 2006
15. ### Simon BiberGuest

wrote:
> Hi friends,
> I need a sample code in C which will convert a Hexadecimal
> number into decimal number. I had written a code for that but it was
> too long, I need a small code, so request u all to provide me with
> small sample code for that.
>

You won't get much smaller than this:

sprintf(d,"%ld",strtol(s,0,16));

It will read from the string s a number in hexadecimal form. It will
convert that number to a long int. It will write that number as a string
in decimal form into the memory starting at d, which must be long enough
to contain any required value.

--
Simon.

Simon Biber, Nov 16, 2006