Results 1 to 9 of 9

Thread: Convert number to binary

  1. #1

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772

    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.

  2. #2
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    cout<<bin(33);

    don't remember the header....stdio.h? math.h?


  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  4. #4

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    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.

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  6. #6
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    You can easily make your own function to convert decimal to binary numbers. Just use the modulus operetor. Very easy.
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  8. #8
    Lively Member
    Join Date
    Dec 2000
    Location
    Indiana
    Posts
    73

    How..?

    How exactly would this function be made?

    void dec2bin(int decimal, char * buffer);

    .... Finish?

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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
  •  



Click Here to Expand Forum to Full Width