What is an easy way to convert a number its binary equivalent? There used to be an example on cplusplus.com using printf() but now it's not there.
Printable View
What is an easy way to convert a number its binary equivalent? There used to be an example on cplusplus.com using printf() but now it's not there.
cout<<bin(33);
don't remember the header....stdio.h? math.h?
:)
what do you need it for? usually the convertion is superfluous itself if it isn't for output
It is for output. I just thought it would be neat to make, because I have seen a converter in javascript and VB, but not in C++.
if it wasn't for the microsoft compilers restricted ability to use template recursion with terminators (or maybe it could be done but i don't know how) i would have coded a neat piece of converter using templates.
I think _itoa lets you specify radix when you convert to string, specify 2
You can easily make your own function to convert decimal to binary numbers. Just use the modulus operetor. Very easy.
I'd use bitshift and bitwise and
How exactly would this function be made?
void dec2bin(int decimal, char * buffer);
.... Finish?
Code:#include "stdlib.h"
#include "stdio.h"
int main(int argc, char* argv[])
{
int i = 123;
printf("%08x\n", i);
char str[33];
_itoa(i, str, 2);
printf("%s\n", str);
return 0;
}