Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Large Decimal to Hex Conversion in C

Reply
Thread Tools

Large Decimal to Hex Conversion in C

 
 
Brad
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
about this would be appreciated.

Thanks,
Brad.
 
Reply With Quote
 
 
 
 
Tom St Denis
Guest
Posts: n/a
 
      07-10-2003
Brad wrote:
> 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
> about this would be appreciated.


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

 
Reply With Quote
 
 
 
 
Glen Herrmannsfeldt
Guest
Posts: n/a
 
      07-10-2003

"Brad" <> wrote in message
news: 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
> about this would be appreciated.


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



 
Reply With Quote
 
Mr. 4X
Guest
Posts: n/a
 
      07-10-2003
(Brad) wrote:

> 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
> about this would be appreciated.
>
> Thanks,
> Brad.


 
Reply With Quote
 
Brad
Guest
Posts: n/a
 
      07-11-2003
Tom St Denis <> wrote in message news:<s_fPa.121340$. cable.rogers.com>...
> Brad wrote:
> > 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
> > about this would be appreciated.

>
> 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.
Brad.
 
Reply With Quote
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      07-11-2003

On Fri, 11 Jul 2003, Brad wrote:
>
> Tom St Denis <> wrote in message news:<s_fPa.121340$. cable.rogers.com>...
> > Brad wrote:
> > > 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
> > > about this would be appreciated.

> >
> > 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

 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      07-12-2003

"Arthur J. O'Dwyer" <> 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.



 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      07-14-2003
(Brad) wrote in message news:<. 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
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      07-14-2003
(Peter Nilsson) writes:

> (Brad) wrote in message news:<. 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.
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      07-14-2003
Ben Pfaff <> wrote in message news:<>...
> (Peter Nilsson) writes:
>
> > (Brad) wrote in message news:<. 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Decimal to Packed Decimal Conversion in C++ Ven C++ 3 08-01-2006 03:56 PM
newbie: hex, decimal, binary conversion bob C Programming 6 03-21-2006 01:51 AM
Hex Color Codes - Hex 6 <=> Hex 3 lucanos@gmail.com HTML 10 08-18-2005 11:21 PM
Hex to dec conversion for large numbers Alex Vinokur C++ 2 11-26-2004 04:52 PM
hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ?? Bengt Richter Python 6 08-19-2003 07:33 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57