Large Decimal to Hex Conversion in C

B

Brad

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

Tom St Denis

Brad said:
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
 
G

Glen Herrmannsfeldt

Brad said:
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
 
M

Mr. 4X

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

Brad

Tom St Denis said:
Brad said:
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.
 
A

Arthur J. O'Dwyer

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
 
M

Malcolm

Arthur J. O'Dwyer said:
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.
 
P

Peter Nilsson

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);
}
 
P

Peter Nilsson

Ben Pfaff said:
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.
 
R

Ravi Uday

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
 
W

W.Paure

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
 
T

Tom St Denis

Ravi said:
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
 
E

EMP

Huh?
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.
 
P

Paul Hsieh

(e-mail address removed) says:
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 */
}
 
B

Brad

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.

First, thanks for all the suggestions and comments about this issue.

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,
Brad.
 
G

Glen Herrmannsfeldt

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

Peter Nilsson

Glen Herrmannsfeldt said:
(snip)


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


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().
 
B

Brad

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.

Brad.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top