Velocity Reviews > Large Decimal to Hex Conversion in C

# Large Decimal to Hex Conversion in C

Guest
Posts: n/a

 07-10-2003
I have a problem in that I need to change a large decimal value into
its hex equivalent. The largest decimal value I need to represent as
hex is 25516777215. The problem here is that this number is larger
than any of the C functions can handle. Any suggestions on how to go

Thanks,

Tom St Denis
Guest
Posts: n/a

 07-10-2003
> I have a problem in that I need to change a large decimal value into
> its hex equivalent. The largest decimal value I need to represent as
> hex is 25516777215. The problem here is that this number is larger
> than any of the C functions can handle. Any suggestions on how to go

Get a big num library?

Do you have the numbers as an ASCII string to begin with? If so than
just read it to a long long and then do the trivial div/mod to get the
digits.

If not you will have to be more clever [minimal hexbignum math ]

Tom

Glen Herrmannsfeldt
Guest
Posts: n/a

 07-10-2003

"Brad" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I have a problem in that I need to change a large decimal value into
> its hex equivalent. The largest decimal value I need to represent as
> hex is 25516777215. The problem here is that this number is larger
> than any of the C functions can handle. Any suggestions on how to go

For numbers that size it isn't hard to do it in a few parts and put the
numbers together. I thought you meant ones with thousands or millions of
digits.

That is, though, a very strange number. Note that 16777215 decimal is
X'ffffff' and 255 decimal is X'ff', so it isn't at all obvious why
25500000000+16777215 would be the largest number you are interested in.

If you divide it by 16777216 you get the higher hex digits, and % 16777216
you get the low digits. Print the two without leading zero supression and
it will look like one number.

-- glen

Mr. 4X
Guest
Posts: n/a

 07-10-2003

> I have a problem in that I need to change a large decimal value into
> its hex equivalent. The largest decimal value I need to represent as
> hex is 25516777215. The problem here is that this number is larger

~25 billion will fit into a 'long long' (64=< bits) variable. A compiler
which supports l;ong long will probably have printf etc. implementations
that can handle these values.

> than any of the C functions can handle. Any suggestions on how to go
>
> Thanks,

Guest
Posts: n/a

 07-11-2003
Tom St Denis <(E-Mail Removed)> wrote in message news:<s_fPa.121340\$(E-Mail Removed). cable.rogers.com>...
> > I have a problem in that I need to change a large decimal value into
> > its hex equivalent. The largest decimal value I need to represent as
> > hex is 25516777215. The problem here is that this number is larger
> > than any of the C functions can handle. Any suggestions on how to go

>
> Get a big num library?
>
> Do you have the numbers as an ASCII string to begin with? If so than
> just read it to a long long and then do the trivial div/mod to get the
> digits.
>
> If not you will have to be more clever [minimal hexbignum math ]
>
> Tom

I had never heard of a "big number library" before and have since been
told to look into that by more than one person. Since I work in an
application development group and have no control over the OS or the
compiler on it I have made a request for our UNIX team to look into
the installation of the add-on. As for the long long data type, we
are using HPUX_11 and it does not seem to support it in its current
configuration. I assume a big number library will support this data
type.

Thanks for all the suggestions, hopefully a compiler add-on will solve
my problem.

Arthur J. O'Dwyer
Guest
Posts: n/a

 07-11-2003

On Fri, 11 Jul 2003, Brad wrote:
>
> Tom St Denis <(E-Mail Removed)> wrote in message news:<s_fPa.121340\$(E-Mail Removed). cable.rogers.com>...
> > > I have a problem in that I need to change a large decimal value into
> > > its hex equivalent. The largest decimal value I need to represent as
> > > hex is 25516777215. The problem here is that this number is larger
> > > than any of the C functions can handle. Any suggestions on how to go

> >
> > Get a big num library?

>
> I had never heard of a "big number library" before and have since been
> told to look into that by more than one person. Since I work in an
> application development group and have no control over the OS or the
> compiler on it I have made a request for our UNIX team to look into
> the installation of the add-on. As for the long long data type, we
> are using HPUX_11 and it does not seem to support it in its current
> configuration.

That's because it's not a C99 compiler. That's perfectly okay.

> I assume a big number library will support this data
> type.

A bignum library will support "big numbers" (that is, arbitrarily large
integers and maybe rationals too). 'long long' is NOT "big numbers."
'long long' is, mathematically speaking, very small numbers indeed!

What a bignum library usually provides is an ADT (abstract data type)
called 'bignum' or 'bigint' or something similar, implemented usually
as a C struct, which can contain arbitrarily large numbers depending
only on how much RAM your machine has.

> Thanks for all the suggestions, hopefully a compiler add-on will solve
> my problem.

You don't need a compiler add-on to solve that problem - all you need
is a bignum package; or if you can't find a good one for free, then
you may need some high-school math skills to write one yourself.

-Arthur

Malcolm
Guest
Posts: n/a

 07-12-2003

"Arthur J. O'Dwyer" <(E-Mail Removed)> wrote in message
>
> What a bignum library usually provides is an ADT (abstract data type)
> called 'bignum' or 'bigint' or something similar, implemented usually
> as a C struct, which can contain arbitrarily large numbers depending
> only on how much RAM your machine has.
>

It doesn't qualify as an "abstract data type" since it it not a container
which hold arbitrary data.

Peter Nilsson
Guest
Posts: n/a

 07-14-2003
(E-Mail Removed) (Brad) wrote in message news:<(E-Mail Removed). com>...
> I have a problem in that I need to change a large decimal value into
> its hex equivalent. The largest decimal value I need to represent as
> hex is 25516777215. The problem here is that this number is larger
> than any of the C functions can handle.

Huh?

#include <stdio.h>

int main()
{
unsigned long long x = 25516777215;
printf("%llu = 0x%llX\n", x, x);
}

--
Peter

Ben Pfaff
Guest
Posts: n/a

 07-14-2003
(E-Mail Removed) (Peter Nilsson) writes:

> (E-Mail Removed) (Brad) wrote in message news:<(E-Mail Removed). com>...
> > I have a problem in that I need to change a large decimal value into
> > its hex equivalent. The largest decimal value I need to represent as
> > hex is 25516777215. The problem here is that this number is larger
> > than any of the C functions can handle.

>
> unsigned long long x = 25516777215;

Perhaps the OP does not have a C99 compiler.

Peter Nilsson
Guest
Posts: n/a

 07-14-2003
Ben Pfaff <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> (E-Mail Removed) (Peter Nilsson) writes:
>
> > (E-Mail Removed) (Brad) wrote in message news:<(E-Mail Removed). com>...
> > > I have a problem in that I need to change a large decimal value into
> > > its hex equivalent. The largest decimal value I need to represent as
> > > hex is 25516777215. The problem here is that this number is larger
> > > than any of the C functions can handle.

> >
> > unsigned long long x = 25516777215;

>
> Perhaps the OP does not have a C99 compiler.

Problem b'long OP.

C99 compilers do exists and it seems a sensible option to change to a
compiler that supports long longs, especially given that many C90
compilers have supported them for quite some time.

--
Peter