0

ints only go to 32 bits, longs to 64bits... so.. what do you do when you are working with a much larger number?

Also, how easy would it be to switch between the binary representation and the hex representation?

NullVoxPopuli
  • 61,906
  • 73
  • 206
  • 352

3 Answers3

11

Use an array. For example:

// Declare a structure containing an array of 4 64-bit integers
struct uint256_t
{
    uint64_t bits[4];
};

// Then, to convert to hex:
uint256_t x;
char hexstring[65];  // needs to be at least 64 hex digits + 1 for the null terminator
sprintf(hexstring, "%016llx%016llx%016llx%016llx", x.bits[0], x.bits[1], x.bits[2], x.bits[3]);
Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
2

ints only go to 32 bits, longs to 64bits... so.. what do you do when you are working with a much larger number?

You use large number libraries.

Also, how easy would it be to switch between the binary representation and the hex representation?

I don't understand the question. A number's a number's a number. Are you asking how to print a number in an certain base? You can format output when using streams like so:

int x = 100;

cout << x << endl; // print decimal value
cout << oct << x << endl; // print octal value
cout << hex << x << endl; // print hexadecimal value

100
0144
0x64
Community
  • 1
  • 1
Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • 1
    Its best to put examples into your answer rather than link to external sites. Yo never know how long the external site reference will stay valid. – Martin York Mar 17 '11 at 19:29
  • Yeah, I don't know that I agree with that across the board. The first link is to an SO page, so yeah, if that's gone so is this one. The second link is to an example, so yeah, I could have posted an example of my own (and done). – Ed S. Mar 17 '11 at 20:16
0

Consider using GMP Arithmetic Library.

Nawaz
  • 353,942
  • 115
  • 666
  • 851