15
Integer.toString(n,8) // decimal to octal

Integer.toString(n,2) // decimal to binary

Integer.toString(n,16) //decimal to Hex

We have these functions in java ... do we have such built-in functions in c++

jwfearn
  • 28,781
  • 28
  • 95
  • 122
coder101
  • 840
  • 2
  • 11
  • 29
  • 2
    No, but you can do it easily with [`std::ostringstream`](http://en.cppreference.com/w/cpp/io/basic_ostringstream) using the correct base [manipulator](http://en.cppreference.com/w/cpp/io/manip) (with the exception of binary format). – Some programmer dude Mar 07 '15 at 19:13
  • There are a *lot* of examples here http://stackoverflow.com/questions/22746429/c-decimal-to-binary-converting – Rollen Mar 07 '15 at 19:13
  • Converting is very simple , i do agree with it , by using a while lopp we can easily convert it i just wanted to know if there is a pre defined function :) – coder101 Mar 07 '15 at 19:22
  • There isn't one function that does it. You have to perform a number of operations. There are tricks to make it short (without a loop) in the link as well as what @JoachimPileborg said but other than that...nothing. – Rollen Mar 07 '15 at 19:26
  • possible duplicate of [How to convert an int to a binary string representation in C++](http://stackoverflow.com/questions/2890502/how-to-convert-an-int-to-a-binary-string-representation-in-c) – Jonathan Mee Mar 09 '15 at 11:38
  • What do you mean "its binary format"? Octal and hex are not binary formats. Do you just mean convert an integer to a string, using a given base? – Jonathan Wakely Mar 09 '15 at 12:05
  • 1
    @JonathanMee, that only answer one third of the question – Jonathan Wakely Mar 09 '15 at 12:06
  • @JonathanWakely Hmm... good comment. I missed that when I first read through. Added an answer as penance. – Jonathan Mee Mar 09 '15 at 15:16
  • @JonathanMee, and it's the only answer that's complete and portable, nice :) – Jonathan Wakely Mar 09 '15 at 15:24
  • @JonathanWakely Thanks to your comments only. I actually thought `iota` was portable. I've even used it in code that was supposed to be portable. You can imagine my shock when I read [at cplusplus.com](http://www.cplusplus.com/reference/cstdlib/itoa/#portability): "This function is not defined in ANSI-C and is not part of C++, but is supported by some compilers." – Jonathan Mee Mar 09 '15 at 15:30
  • 1
    @JonathanMee, it's Windows only AFAIK (and first edition UNIX, but nothing since). Confusion isn't helped by the completely bogus statement in MSDN ["This POSIX function is deprecated. "](https://msdn.microsoft.com/en-us/library/ms235327.aspx) because it has never been in any POSIX standard! – Jonathan Wakely Mar 09 '15 at 16:11
  • @JonathanWakely Wow well done, that is the exact article that I sourced when I wrote my "cross platform" `iota` code. – Jonathan Mee Mar 09 '15 at 17:15

5 Answers5

25

You can use std::bitset to convert a number to its binary format.

Use the following code snippet:

  std::string binary = std::bitset<8>(n).to_string();
  • 4
    That only answers one third of the question. – Jonathan Wakely Mar 09 '15 at 12:06
  • 3
    @NilutpalBorgohain It found that this will convert the integer `n` to its binary with 8 binary digits. What if I want to do this at run time and not at compile time? – asn Jan 18 '19 at 13:14
5

There is one function available itoa present in the stdlib.h by which we can convert integer to string. It is not exactly defined in C or C++ but supported by many compilers.

char *  itoa ( int value, char * str, int base );

itoa example

#include <iostream>
#include <stdlib.h>

int main ()
{
    int i;
    char buffer [33];
    printf ("Enter a number: ");
    scanf ("%d",&i);
    itoa (i,buffer,10);
    printf ("decimal: %s\n",buffer);
    itoa (i,buffer,16);
    printf ("hexadecimal: %s\n",buffer);
    itoa (i,buffer,2);
    printf ("binary: %s\n",buffer);
    return 0;
}

OUTPUT

Enter a number: 1750
decimal: 1750
hexadecimal: 6d6
binary: 11011010110

For more details you can refer itoa

Vivek Mahto
  • 194
  • 1
  • 17
3

If you need a cross platform way to do this cplusplus.com suggests: sprintf is a good option for hex and oct:

int Integer = 13;
char toOct[sizeof(int) * (unsigned int)(8.0f / 3.0f) + 2];
char toHex[sizeof(int) * 8 / 4 + 1];
bitset<sizeof(int)> toBin(Integer);

sprintf(toOct, "%o", Integer);
sprintf(toHex, "%x", Integer);

cout << "Binary: " << toBin << "\nOctal: " << toOct << "\nDecimal: " << Integer << "\nHexadecimal: " << toHex << endl;

Note that toOct and toHex are char arrays sized to hold the largest integer in Octal and Hexadecimal strings respectively, so there is no need for dynamic resizing.

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
0
int n = 64;
string binary = bitset<64>(n).to_string();
binary.erase(0, binary.find_first_not_of('0'));
cout << binary << endl;
ZAFIR AHMAD
  • 79
  • 1
  • 3
  • It's actually pretty good as it trims all the leading bits. It'd be better if we could get it in 1 step though. – cryo May 30 '22 at 15:11
0
 int main() {
    int n;
    cin >> n;
    string binary = bitset<32>(n).to_string();
    cout << binary << endl;
    return 0;
}

Expected output: 00000000000000000000000000010000

Aaqib Javaid
  • 303
  • 2
  • 5