Large Decimal to Hex Conversion in C

Discussion in 'C Programming' started by Brad, Jul 10, 2003.

  1. Brad

    Brad Guest

    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.
    Brad, Jul 10, 2003
    #1
    1. Advertising

  2. Brad

    Tom St Denis Guest

    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
    Tom St Denis, Jul 10, 2003
    #2
    1. Advertising

  3. "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. 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
    Glen Herrmannsfeldt, Jul 10, 2003
    #3
  4. Brad

    Mr. 4X Guest

    (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.
    Mr. 4X, Jul 10, 2003
    #4
  5. Brad

    Brad Guest

    Tom St Denis <> wrote in message news:<s_fPa.121340$>...
    > 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.
    Brad, Jul 11, 2003
    #5
  6. On Fri, 11 Jul 2003, Brad wrote:
    >
    > Tom St Denis <> wrote in message news:<s_fPa.121340$>...
    > > 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
    Arthur J. O'Dwyer, Jul 11, 2003
    #6
  7. Brad

    Malcolm Guest

    "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
    #7
  8. (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);
    }

    --
    Peter
    Peter Nilsson, Jul 14, 2003
    #8
  9. Brad

    Ben Pfaff Guest

    (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
    #9
  10. 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
    #10
  11. Brad

    Ravi Uday Guest

    (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
    #11
  12. Brad

    W.Paure Guest

    (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
    #12
  13. Brad

    Tom St Denis Guest

    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
    #13
  14. Brad

    EMP Guest

    > > 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
    #14
  15. Brad

    Paul Hsieh Guest

    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
    #15
  16. Brad

    Brad Guest

    (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. 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.
    Brad, Jul 18, 2003
    #16
  17. "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.

    -- glen
    Glen Herrmannsfeldt, Jul 19, 2003
    #17
  18. "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
    #18
  19. Brad

    Brad Guest

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

    Brad.
    Brad, Jul 22, 2003
    #19
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,189
    Neredbojias
    Aug 19, 2005
  2. Alex Vinokur
    Replies:
    2
    Views:
    5,104
    Basil
    Nov 26, 2004
  3. Bengt Richter
    Replies:
    6
    Views:
    464
    Juha Autero
    Aug 19, 2003
  4. bob
    Replies:
    6
    Views:
    747
    Peter Shaggy Haywood
    Mar 21, 2006
  5. Ven
    Replies:
    3
    Views:
    1,322
Loading...

Share This Page