# Large Decimal to Hex Conversion in C

Discussion in 'C Programming' started by Brad, Jul 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,

2. ### Tom St DenisGuest

> 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

Tom St Denis, Jul 10, 2003

3. ### Glen HerrmannsfeldtGuest

news:...
> 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

Glen Herrmannsfeldt, Jul 10, 2003
4. ### Mr. 4XGuest

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

Mr. 4X, Jul 10, 2003

Tom St Denis <> wrote in message news:<s_fPa.121340\$>...
> > 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.

6. ### Arthur J. O'DwyerGuest

On Fri, 11 Jul 2003, Brad wrote:
>
> Tom St Denis <> wrote in message news:<s_fPa.121340\$>...
> > > 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

Arthur J. O'Dwyer, Jul 11, 2003
7. ### MalcolmGuest

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

Malcolm, Jul 12, 2003
8. ### Peter NilssonGuest

> 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

Peter Nilsson, Jul 14, 2003
9. ### Ben PfaffGuest

(Peter Nilsson) writes:

> (Brad) wrote in message news:<>...
> > 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.

Ben Pfaff, Jul 14, 2003
10. ### Peter NilssonGuest

Ben Pfaff <> wrote in message news:<>...
> (Peter Nilsson) writes:
>
> > (Brad) wrote in message news:<>...
> > > 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

Peter Nilsson, Jul 15, 2003
11. ### Ravi UdayGuest

(Peter Nilsson) wrote in message news:<>...
> (Brad) wrote in message news:<>...
> > 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);
> }

when i try to compile your code on MSVC I get an error saying
"error C2632: 'long' followed by 'long' is illegal"
What is the problem,do i need to change something ??

- Ravi

Ravi Uday, Jul 15, 2003
12. ### W.PaureGuest

(Ravi Uday) wrote in message news:<>...
> (Peter Nilsson) wrote in message news:<>...
> > (Brad) wrote in message news:<>...
> > > 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);
> > }

>
> when i try to compile your code on MSVC I get an error saying
> "error C2632: 'long' followed by 'long' is illegal"
> What is the problem,do i need to change something ??
>
> - Ravi

Yes,it is new type in c99.
try gcc 3

W.Paure, Jul 15, 2003
13. ### Tom St DenisGuest

Ravi Uday wrote:
> (Peter Nilsson) wrote in message news:<>...
>
>> (Brad) wrote in message news:<>...
>>
>>>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);
>> }

>
>
> when i try to compile your code on MSVC I get an error saying
> "error C2632: 'long' followed by 'long' is illegal"
> What is the problem,do i need to change something ??

In both of my LibTom* libs I use

#if defined(_MSC_VER) || defined(__BORLANDC__)
typedef unsigned __int64 ulong64;
typedef signed __int64 long64;
#else
typedef unsigned long long ulong64;
typedef signed long long long64;
#endif

So now "ulong64" is a fairly portable [amongst gcc, bcc and msvc] data
type for 64-bit longs.

MSVC doesn't have obvious support for printf'ing a long long but you
can portably work around that with unsigned longs

printf("%lx%08lx", (unsigned long)(x>>32), (unsigned long)(x & 0xFFFFFFFF));

Tom

Tom St Denis, Jul 15, 2003
14. ### EMPGuest

> > Huh?
> >
> > #include <stdio.h>
> >
> > int main()
> > {
> > unsigned long long x = 25516777215;
> > printf("%llu = 0x%llX\n", x, x);
> > }

>
> when i try to compile your code on MSVC I get an error saying
> "error C2632: 'long' followed by 'long' is illegal"
> What is the problem,do i need to change something ??
>

MS Visual C does not implement "long long" type (by that name). They have
to be different, you know.
Their basic 64-bit data types are:
- __int64 / INT64 / LONG64
- unsigned __int64 / UINT64 / ULONG64 / DWORD64

They're defined in basetsd.h

emp.

EMP, Jul 15, 2003
15. ### Paul HsiehGuest

says:
> (Peter Nilsson) wrote in message news:<>...
> > (Brad) wrote in message news:<>...
> > > 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);
> > }

>
> when i try to compile your code on MSVC I get an error saying
> "error C2632: 'long' followed by 'long' is illegal"
> What is the problem,do i need to change something ??

MSVC, and WATCOM C/C++ do not support "long long". Your inline constant needs
some indicator on it that its 64bits, to avoid to being truncated down to an
integer. The right way to code this on these proprietary compilers is as
follows:

#include <stdio.h>

int main() {
__int64 x = 25516777215I64; /* The I64 at the end is optional. */
printf ("%I64d = 0x%I64X\n", x, x);
return 0; /* Bad form to leave this out */
}

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sourceforge.net/

Paul Hsieh, Jul 16, 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,

I am using HP-UX v11 and the C++ compiler that comes with it. I found
by posting to different places and talking to our UNIX team that
placing a +DD64 flag on the command line enables large data types to
be available. After implementing this flag the unsigned long long
worked as it should.

I did however have a problem trying to get the string containing the
value into the unsigned long long. What I ended up doing was using
"(unsigned long long)atof(str)". This did the conversion for me and
then truncated the decimal places leaving just the value I was looking
for. Problem solved!

Thanks again,

17. ### Glen HerrmannsfeldtGuest

news:...

(snip)

> I am using HP-UX v11 and the C++ compiler that comes with it. I found
> by posting to different places and talking to our UNIX team that
> placing a +DD64 flag on the command line enables large data types to
> be available. After implementing this flag the unsigned long long
> worked as it should.

I hope you were using the C option to such compiler...

> I did however have a problem trying to get the string containing the
> value into the unsigned long long. What I ended up doing was using
> "(unsigned long long)atof(str)". This did the conversion for me and
> then truncated the decimal places leaving just the value I was looking
> for. Problem solved!

If long long support is included, sscanf(str,"%LLd",&llvar); should do it.

-- glen

Glen Herrmannsfeldt, Jul 19, 2003
18. ### Peter NilssonGuest

"Glen Herrmannsfeldt" <> wrote in message news:<8J4Sa.82183\$GL4.20933@rwcrnsc53>...
> "Brad" <> wrote in message
> news:...
>
> (snip)
>
> > I am using HP-UX v11 and the C++ compiler that comes with it. I found
> > by posting to different places and talking to our UNIX team that
> > placing a +DD64 flag on the command line enables large data types to
> > be available. After implementing this flag the unsigned long long
> > worked as it should.

>
> I hope you were using the C option to such compiler...
>
> > I did however have a problem trying to get the string containing the
> > value into the unsigned long long. What I ended up doing was using
> > "(unsigned long long)atof(str)". This did the conversion for me and
> > then truncated the decimal places leaving just the value I was looking
> > for. Problem solved!

>
> If long long support is included, sscanf(str,"%LLd",&llvar); should do it.

Is "%LLd" a platform specific alternative to "%lld"? In any case, a
more robust alternative to sscanf() would be strtoll() and strtoull().

--
Peter

Peter Nilsson, Jul 20, 2003

(Peter Nilsson) wrote in message news:<>...
> "Glen Herrmannsfeldt" <> wrote in message news:<8J4Sa.82183\$GL4.20933@rwcrnsc53>...
> > "Brad" <> wrote in message
> > news:...
> >

> > If long long support is included, sscanf(str,"%LLd",&llvar); should do it.

>
> Is "%LLd" a platform specific alternative to "%lld"? In any case, a
> more robust alternative to sscanf() would be strtoll() and strtoull().

It seems that HP-UX v11 does not have a strtoll() or a strtoull()
function available, sscanf is available though. I had been using the
%lld formatter throughout this and it seems to work correctly.
Thanks.