3

I have a binary number which I want to represent in decimal. The number is as follows:

Binary = 1000 11100000 00000000 00000000 00000000 00000000 01101110 10110000 10100101  

Its equivalent hexadecimal representation is as follows :

Hexadecimal = 08 E0 00 00 00 00 6E B0 A5

I am working on Windows 7 and tried using calculator in programmer mode with Bin (Binary) and Qword. However, it has a limit of 64 bit.

Questions:

  • How do I represent this number in decimal?
  • Is there any free tool available to represent this number?
  • Is there any way in C++11/C++ to achieve this?
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Atul
  • 745
  • 3
  • 9
  • 17
  • Try this:: http://www.openssl.org/docs/crypto/bn.html. BTW why do you want to convert such a long binary in decimal? What is the purpose? – Abhineet Oct 22 '12 at 05:20
  • 1
    This link covers a number of libraries for representing big numbers... http://stackoverflow.com/questions/1047203/best-bignum-library-to-solve-project-euler-problems-in-c – combinatorial Oct 22 '12 at 05:21
  • 1
    do you need to perform this conversion only once or you want to implement the algorithm? – Serge Oct 22 '12 at 05:29
  • I want to implement an algorithm – Atul Oct 22 '12 at 05:30

3 Answers3

5

First determine how large a binary number your platform will support. Let's say it's 64. Divide your binary number into chunks of 64 and do the low-order chunk as normal. Now, the low-order bit of the next chunk represents 264. Convert that chunk like a regular number but multiply it by 264. You could use this technique as many times as you need. The next chunk you would multiply by 2128, then the next by 2192, etc.

  • Could you explain using the binary number i have mentioned above please ? – Atul Oct 22 '12 at 05:26
  • 1
    Your answer assumes that a big-integer library is used, correct? Or how do you multiply by `2**64`? – jogojapan Oct 22 '12 at 05:28
  • The problem is that using the widest integral type available at the platform to implement big math you do not make the things not simpler. Please think of implementation of multiplication of two 64-bit integers. – Serge Oct 22 '12 at 05:43
  • An example (with the binary number above) will be appreciated – Atul Oct 22 '12 at 05:44
0

I think you should refer this.

You can use the BigInteger class if you are going for JAVA,here.

Or download this user-defined library for performing calculations over big numbers in C++, here.

akshaykumar6
  • 2,147
  • 4
  • 18
  • 31
  • 4
    An answer consisting of 'just a link' is not encouraged. You should give some salient information about what the link contains, at least, because links are prone to link rot. – Jonathan Leffler Oct 22 '12 at 05:26
0

The easier solution may be to work from the other side. Generate the binary representations of all powers of 10: 0x1, 0xA, 0x64, 0x3E8, 0x2710, ... until the power of 10 is larger than your number. Then, count how many times each power of 10 occurs (up to 9, of course).

So 0x2F would be 4 * 0xA + 7 * 0xA, i.e. 47 decimal.

MSalters
  • 173,980
  • 10
  • 155
  • 350