473,383 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Large Decimal to Hex Conversion in C

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.
Nov 13 '05 #1
18 11513
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

Nov 13 '05 #2

"Brad" <br******@mobility.com> wrote in message
news:7f**************************@posting.google.c 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

Nov 13 '05 #3
br******@mobility.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
~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.


Nov 13 '05 #4
Tom St Denis <to********@iahu.ca> wrote in message news:<s_*********************@news04.bloor.is.net. 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.
Nov 13 '05 #5

On Fri, 11 Jul 2003, Brad wrote:

Tom St Denis <to********@iahu.ca> wrote in message news:<s_*********************@news04.bloor.is.net. 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

Nov 13 '05 #6

"Arthur J. O'Dwyer" <aj*@andrew.cmu.edu> 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.

Nov 13 '05 #7
br******@mobility.com (Brad) wrote in message news:<7f**************************@posting.google. 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
Nov 13 '05 #8
ai***@acay.com.au (Peter Nilsson) writes:
br******@mobility.com (Brad) wrote in message news:<7f**************************@posting.google. 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.
Nov 13 '05 #9
Ben Pfaff <bl*@cs.stanford.edu> wrote in message news:<87************@pfaff.Stanford.EDU>...
ai***@acay.com.au (Peter Nilsson) writes:
br******@mobility.com (Brad) wrote in message news:<7f**************************@posting.google. 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
Nov 13 '05 #10
ai***@acay.com.au (Peter Nilsson) wrote in message news:<63**************************@posting.google. com>...
br******@mobility.com (Brad) wrote in message news:<7f**************************@posting.google. 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);
}


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
Nov 13 '05 #11
ra*****@yahoo.com (Ravi Uday) wrote in message news:<ec**************************@posting.google. com>...
ai***@acay.com.au (Peter Nilsson) wrote in message news:<63**************************@posting.google. com>...
br******@mobility.com (Brad) wrote in message news:<7f**************************@posting.google. 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);
}


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
Nov 13 '05 #12
Ravi Uday wrote:
ai***@acay.com.au (Peter Nilsson) wrote in message news:<63**************************@posting.google. com>...
br******@mobility.com (Brad) wrote in message news:<7f**************************@posting.google. 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);
}

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

Nov 13 '05 #13
EMP
> > 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.
Nov 13 '05 #14
ra*****@yahoo.com says:
ai***@acay.com.au (Peter Nilsson) wrote in message news:<63**************************@posting.google. com>...
br******@mobility.com (Brad) wrote in message news:<7f**************************@posting.google. 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);
}


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/
Nov 13 '05 #15
br******@mobility.com (Brad) wrote in message news:<7f**************************@posting.google. 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
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.
Nov 13 '05 #16

"Brad" <br******@mobility.com> wrote in message
news:7f**************************@posting.google.c om...

(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
Nov 13 '05 #17
"Glen Herrmannsfeldt" <ga*@ugcs.caltech.edu> wrote in message news:<8J4Sa.82183$GL4.20933@rwcrnsc53>...
"Brad" <br******@mobility.com> wrote in message
news:7f**************************@posting.google.c om...

(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
Nov 13 '05 #18
ai***@acay.com.au (Peter Nilsson) wrote in message news:<63**************************@posting.google. com>...
"Glen Herrmannsfeldt" <ga*@ugcs.caltech.edu> wrote in message news:<8J4Sa.82183$GL4.20933@rwcrnsc53>...
"Brad" <br******@mobility.com> wrote in message
news:7f**************************@posting.google.c om...
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.

Brad.
Nov 13 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
7
by: BA | last post by:
Hello, I have a string with a price in: "$14.95" I need to get it into a decimal. So I regex 'd the string and dumped the $. Debug mon shows a clean string "14.95" Then I do a...
2
by: Alex Vinokur | last post by:
I am looking for an algorithm/method which enables to convert very large hexadecimal numbers to decimal numbers. Here is what I would like to do. string hex_str = "123456789abcdef0.........."...
10
by: Tuvas | last post by:
I've been thinking about writing a program to generate the world's largest prime numbers, just for the fun of it. This would require being able to hold an 8000000 digit number into memory (25...
4
by: oddstray | last post by:
Hi, I have a number which is larger than the max unsigned long int. I don't have 64-bit integers available to me. I need to get the resulting 40-bit hex string. I can't find any algorithm...
26
by: kerravon | last post by:
The following C program: int main(void) { int x = -2147483648; return (0); } Produces the following warning:
23
by: neha_chhatre | last post by:
which is the best format specifier(data type) if i have to work with decimal number. also please tell me the syntax for truncating a decimal number please reply as soon as possible
6
by: Terry Reedy | last post by:
Gerhard Häring wrote: The new fractions module acts differently, which is to say, as most would want. True Traceback (most recent call last): File "<pyshell#20>", line 1, in <module> F(1.0)...
4
by: =?Utf-8?B?THVpZ2k=?= | last post by:
Hi all, I have an expression like this: decimal? ValoreComplessivoNettoDelfondoValue = (totaleAttivitaValue.HasValue || totalePassivitaValue.HasValue) ? ((totaleAttivitaValue ?? 0) -...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.