Thread: Need help representing a very large number

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    124

    Need help representing a very large number

    Basically what I need is an integer that is equal to 2 left-shifted 160 times, so something like 2 << 160. The problem with that is apparently you can't shift something more than 30 times. I found a way to represent it as a string, which looks like this:
    Code:
    unsigned char pbmodulus[21];
    memset(pbmodulus, 0, sizeof(pbmodulus));
    pbmodulus[0] = 0x01;
    The problem with that is I need it in integer form, and neither atoi nor strtol work. Any suggestions?

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I assume what you mean when you say you need it in integer form is that you need to perform some mathematical operations on it. In that case, you'll either need a library that can handle such large numbers (gmp is a good one) or you'll need to write your own code to do the same thing.

    An 'int' type can generally hold 4 bytes of data (at least on a 32-bit machine) which means 2^32 values. Your compiler might support something non-standard like long long which would probably hold 64 bits, but that still wouldn't be sufficient in this case.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    I was afraid of that, do you by chance know a way to represent that number as a hex string? I thought my string code would be hex, but when I try to import it into a 3rd party function that's supposed to accept hex strings, it says it's improperly formatted. When I tried to e-mail the guy that made the library he told me that 0x01 wasn't a valid hex character. Is that true?

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I'm not sure what you mean. 0x01 is a hex number, but not if you want it to be a character in a string representing a hex number (e.g., "FF").
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allocating a number of large 3d arrays in C
    By Surrender in forum C Programming
    Replies: 22
    Last Post: 08-19-2008, 08:36 AM
  2. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM