Results 1 to 10 of 10

Thread: C/C++ hex integers? super long integers?

  1. #1

    Question C/C++ hex integers? super long integers?

    I have a question about C/C++ and it's usage of variable integers. Can I, and how, use hex and or display as hex when input/output occurs with numbers. Also, for numbers that are huge (Ie: a 1024 bit number), how do I store them in a variable(s). For the kind of keys I am needing to generate, I need big numbers.
    E, the modern pi.

  2. #2
    Senior Member
    Join Date
    Jul 2003
    Im a bit confused what you want to do, storing your large numbers not difficult, what do you want the hex for?

    to print hex out you can do something like: -

    int number = 100;
    printf("In hex the number is %x\n", number);

    need more of a discription to help you sorry,


    ps - (just out of curiousity what type of encryption algothim you using??)

  3. #3
    Senior Member
    Join Date
    Dec 2003
    Pacific Northwest
    Yep you can, but not being specific in your questions makes it harder for us to help you.
    If you want to specify an integer constant as a hexadecimal constant it must begin with 0x (that is a zero with the x)

    Additionally to what i2c provided:

    printf("In hex the number is %x\n", number);
    If you want to specify a char in hex, you must follow the backslash with an x then the number:

    #include <stdio.h>

    int main(void)
    printf("\xB2, \xA3");

    return 0;

    In the 32 bit environment integers can range from 2,147,483,648 to the opposite minus one of course: 2,147,483,647. And always float, double, and long double, can really hold large values, float = 7 digits, double = 15 digits , and long double = 19 digits, so that is probably where you'd be heading.

    more info needed.... but here's a sight with stuff:


    edit: just a note: c/c++ only sets the lower of each data type, not its size in bytes and of course your complier comes in to play as well. It specifices the range/size of the types found in your header <climits>. So your complier may not accept your code and if it does your computer may not be able run it. Here's what just "long" looks like: 8 bytes, -9223372036854775808 to 9223372036854775807

  4. #4
    Senior Member
    Join Date
    Feb 2004

    I forgot what it's called, but there is a way to deal with very large numbers (as large as you want) by using strings. But since it's a string, you would have to write your own functions to add, subtract, multiply, etc. I'm sure there is some source code out there for it but I don't really know what to search.

    It all depends on the size of numbers you want to use. As Relyt said, long doubles can hold 19 digits. Is that long enough?

    Ie: a 1024 bit number
    Erm... How big is that?

    Anyone knows what it's called? (using numbers in strings)



  5. #5

    large numbers/hex in c++

    Well, I am using RSA encryption, which as you can imagine requires some very large numbers. With those size of numbers, I was also wondering about the hex, just for ease of displaying the numbers, since in decimal format they'd be HUGE.
    E, the modern pi.

  6. #6


    Also, someone said something about using strings and writing my own functions. I don't know how I would do that. I need to be able to add,subtract,multiply,divide,mod,and do exponents. How do I do that with strings and such?
    E, the modern pi.

  7. #7
    Senior Member
    Join Date
    Oct 2001
    I think you're looking for arbitrary precision? (Allows you to store arbitraryly large numbers, based on avaliable resources, etc)

    If that is what you are looking for, you now have the word to search. Acturally writing a good arbitrary precision calculator is probably beyond what you need, but I'm sure that Googling can turn up a few that you could use for free. I know that I'd have a hard time writing my own, so unless you know what you are getting into I suggest searching for one and learning how to use it...

    BTW, to help inform you as much as I can in this post, using strings and either linked-lists (pointers) or large arrays, you can represent large numbers with one digit per variable, ie one for the ones, tens, hundreds, etc. Kind of ineffecient in storage space, but it can get the job done as long as your not trying to do something so extreme as to stress the power of huge super computers in memory constraints with needs of hundreds of GB of RAM...

    Hope this helps you. Although it may need some tweaking to load/convert numbers or keys from a file on the disk if you have to convert between HEX, Decimal, Binary, or any other numbering system...

  8. #8
    Senior Member
    Join Date
    Jul 2003
    I was thinking of the one digit per variable too, especially if you are using an array that would hold the number. for a


    array, longNumber[0] would be the highest order digit, while longNumber[1023] would be the unit digit. I recommend using a

    char longNumber[1024]

    array, because you are only using digits [0-9] which can be easily represented in one-byte. I am sure you know that if you were to do it with an int it would take 4 times more space... let's not even go into all the ther stuff [long, double etc].

    You will need to use the atoi() and itoa() functions in math.h.

    atoi ASCII to Integer
    itoa Integer to ASCII

    I think they are self-explanatory. A reference book will show you the appropriate syntax and what not.

    The tricky part will be manipulating these arrays. Not that tricky, if coding is something you like. Adding and substracting are trivial, multiplying, dividing and mod-ing might be a bit of a hassle. Exponents on 1024-bit long numbers is going to be a mental strain [and I am not even asking what computer you have to do this]

    Also you might want to try unsigned type variables. In this way the number start at 0 and goes to twice the range specified by Relyt. Maybe that will help out.

    Depending on the nature of your program I recommend avoiding the usage of arbitrary precission to which all floating-point routines are susceptible of. Coding your own large-number algorithm might be tedious, but it will offer more accuracy [and it is re-usable ].

    I hope this is helpful... I'll try n help out as I can onve you post more information/questions.


  9. #9


    Thanks, yea I decided to write my own functions for adding/subtracting, multiplying/dividing, modding, and powers. Dividing and modding are the really hard ones. Those atoi and itoa functions will help a lot. Again, thanks.
    E, the modern pi.

  10. #10
    Senior Member
    Join Date
    Jul 2003
    Glad to be able to help. I hope there will be no bugs/problems or anything, but don't hesitate to post back [or PM me] about whatever else you encounter.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts