|
-
Aug 31st, 2001, 06:53 PM
#1
Thread Starter
Fanatic Member
Convert number to binary
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.
Alcohol & calculus don't mix.
Never drink & derive.
-
Aug 31st, 2001, 07:02 PM
#2
Frenzied Member
cout<<bin(33);
don't remember the header....stdio.h? math.h?
-
Aug 31st, 2001, 07:08 PM
#3
transcendental analytic
what do you need it for? usually the convertion is superfluous itself if it isn't for output
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Aug 31st, 2001, 07:23 PM
#4
Thread Starter
Fanatic Member
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++.
Alcohol & calculus don't mix.
Never drink & derive.
-
Aug 31st, 2001, 07:35 PM
#5
transcendental analytic
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
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Sep 1st, 2001, 06:23 AM
#6
Frenzied Member
You can easily make your own function to convert decimal to binary numbers. Just use the modulus operetor. Very easy.
-
Sep 1st, 2001, 07:26 AM
#7
transcendental analytic
I'd use bitshift and bitwise and
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Sep 6th, 2001, 03:49 PM
#8
Lively Member
How..?
How exactly would this function be made?
void dec2bin(int decimal, char * buffer);
.... Finish?
-
Sep 7th, 2001, 05:13 AM
#9
itoa works
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;
}
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|